This repository was archived by the owner on Jul 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
3635fbb
Update README
akim-bow 7a77b34
Improve build experience
akim-bow be922b4
Merge branch 'master' into DXJ-490
akim-bow 3259168
Fix eslint
akim-bow e8a05a5
Merge remote-tracking branch 'origin/DXJ-490' into DXJ-490
akim-bow 484f15d
Fix eslint
akim-bow 48c9951
Fix eslint
akim-bow c9b4d34
Fix tooling
akim-bow 45fbe16
Fix formatting
akim-bow ddb07d1
Fix formatting
akim-bow 93423a3
Fix test resource name
akim-bow 9bc2b3e
Remove async
akim-bow 1fdc9cb
Fix comments
akim-bow c1db062
Add ts-check
akim-bow 2c4940c
Add new line
akim-bow 86d6eb2
Fix arg
akim-bow 969062b
add todo's
akim-bow 8cb3ab8
Fix lint
akim-bow 730e78e
Fix typo
akim-bow 5ac94d9
Type module
akim-bow 9b8b950
Add deps to isomorphic package
akim-bow 083d964
Extract function type
akim-bow 94f0e43
Fix newline
akim-bow 036a1e5
Remove private
akim-bow 633d206
Use prepare hook instead of postinstall
akim-bow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ node_modules/ | |
# Build directory | ||
**/dist | ||
**/public | ||
.DS_Store |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,6 @@ pnpm-lock.yaml | |
**/build | ||
**/public | ||
|
||
**/CHANGELOG.md | ||
**/CHANGELOG.md | ||
|
||
packages/core/js-client/src/versions.ts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,72 @@ | ||
# Fluence JS Client | ||
|
||
[](https://www.npmjs.com/package/@fluencelabs/js-client.api) | ||
[](https://www.npmjs.com/package/@fluencelabs/js-client.web.standalone) | ||
[](https://www.npmjs.com/package/@fluencelabs/js-client) | ||
|
||
This is the Javascript client for the [Fluence](https://fluence.network) network. The main role of the JS client is to connect to the Fluence Network and allow you to integrate Aqua code into your application. | ||
|
||
## Installation | ||
|
||
Adding the Fluence JS client for your web application is very easy. | ||
> JS Client only supports the ESM format that means not every Node.js project can install it. | ||
> You can read more [here](https://nodejs.org/api/esm.html) | ||
|
||
### Browser-based Apps | ||
1. Install the client: | ||
|
||
1. Add a script tag with the JS Client bundle to your `index.html`. The easiest way to do this is using a CDN (like [JSDELIVR](https://www.jsdelivr.com/) or [UNPKG](https://unpkg.com/)). The script is large, thus we highly recommend to use the `async` attribute. | ||
|
||
Here is an example using the JSDELIVR CDN: | ||
|
||
```html | ||
<head> | ||
<title>Cool App</title> | ||
<script | ||
src="https://cdn.jsdelivr.net/npm/@fluencelabs/[email protected]/dist/js-client.min.js" | ||
async | ||
></script> | ||
</head> | ||
``` | ||
|
||
If you cannot or don't want to use a CDN, feel free to get the script directly from the [npm package](https://www.npmjs.com/package/@fluencelabs/js-client.web.standalone) and host it yourself. You can find the script in the `/dist` directory of the package. (Note: this option means that developers understand what they are doing and know how to serve this file from their own web server.) | ||
|
||
2. Install the following packages: | ||
|
||
``` | ||
npm i @fluencelabs/js-client.api @fluencelabs/fluence-network-environment | ||
```bash | ||
npm i @fluencelabs/js-client | ||
``` | ||
|
||
3. Add the following lines at the beginning of your code: | ||
2. Add the following lines at the beginning of your code: | ||
|
||
``` | ||
import { Fluence } from "@fluencelabs/js-client.api"; | ||
import { randomKras } from '@fluencelabs/fluence-network-environment'; | ||
```javascript | ||
import { Fluence, randomKras } from "@fluencelabs/js-client"; | ||
|
||
Fluence.connect(randomKras()); | ||
``` | ||
|
||
### Node.js Apps | ||
|
||
**Prerequisites:** | ||
### HTML page | ||
|
||
The Fluence JS Client only supports the ESM format. This implies that a few preliminary steps are required if your project is not already using ESM: | ||
Add a script tag with the JS Client bundle to your `index.html`. The easiest way to do this is using a CDN ( | ||
like [JSDELIVR](https://www.jsdelivr.com/) or [UNPKG](https://unpkg.com/)). | ||
|
||
- Add `"type": "module"` to your package.json. | ||
- Replace `"main": "index.js"` with `"exports": "./index.js"` in your package.json. | ||
- Remove `'use strict';` from all JavaScript files. | ||
- Replace all `require()`/`module.export` with `import`/`export`. | ||
- Use only full relative file paths for imports: `import x from '.';` → `import x from './index.js';`. | ||
Here is an example using the JSDELIVR CDN: | ||
|
||
If you are using TypeScript: | ||
|
||
- Make sure you are using TypeScript 4.7 or later. | ||
- Add [`"module": "ESNext", "target": "ESNext", "moduleResolution": "nodenext"`](https://www.typescriptlang.org/tsconfig#module) to your tsconfig.json. | ||
- Use only full relative file paths for imports: `import x from '.';` → `import x from './index.js';`. | ||
- Remove `namespace` usage and use `export` instead. | ||
- You must use a `.js` extension in relative imports even though you're importing `.ts` files. | ||
```html | ||
<head> | ||
<title>Cool App</title> | ||
<script src="https://cdn.jsdelivr.net/npm/@fluencelabs/js-client/dist/browser/index.min.js"></script> | ||
</head> | ||
``` | ||
|
||
**Installation:** | ||
If you cannot or don't want to use a CDN, feel free to get the script directly from | ||
the [npm package](https://www.npmjs.com/package/@fluencelabs/js-client) and host it yourself. You can find the script in | ||
the `/dist` directory of the package. (Note: this option means that developers understand what they are doing and know | ||
how to serve this file from their own web server.) | ||
|
||
1. Install the following packages: | ||
After importing JS-client to HTML page the client is available as `window.Fluence` variable. | ||
To get a specific network you can peek at | ||
|
||
``` | ||
npm i @fluencelabs/js-client.api"@fluencelabs/js-client.node @fluencelabs/fluence-network-environment | ||
``` | ||
|
||
2. Add the following lines at the beginning of your code: | ||
``` | ||
https://cdn.jsdelivr.net/npm/@fluencelabs/js-client/dist/network.js | ||
``` | ||
|
||
``` | ||
import '@fluencelabs/js-client.node'; | ||
import { Fluence } from "@fluencelabs/js-client.api"; | ||
import { randomKras } from '@fluencelabs/fluence-network-environment'; | ||
and hardcode selected network. So initialization would look like this | ||
|
||
Fluence.connect(randomKras()); | ||
``` | ||
```javascript | ||
// Passing 1 kras network config from ./dist/network.js above | ||
window.Fluence.connect({ | ||
multiaddr: | ||
"/dns4/0-kras.fluence.dev/tcp/9000/wss/p2p/12D3KooWSD5PToNiLQwKDXsu8JSysCwUt8BVUJEqCHcDe7P5h45e", | ||
peerId: "12D3KooWSD5PToNiLQwKDXsu8JSysCwUt8BVUJEqCHcDe7P5h45e", | ||
}); | ||
``` | ||
|
||
## Usage in an Application | ||
|
||
Once you've added the client, you can compile [Aqua](https://github.com/fluencelabs/aqua) and run it in your application. To compile Aqua, use [Fluence CLI](https://github.com/fluencelabs/cli). | ||
|
||
1. Install the package: | ||
|
||
``` | ||
```bash | ||
npm i -D @fluencelabs/cli | ||
``` | ||
|
||
|
@@ -130,7 +110,7 @@ Once you've added the client, you can compile [Aqua](https://github.com/fluencel | |
6. Now you can import and call Aqua code from your application like | ||
this: | ||
|
||
``` | ||
```javascript | ||
import { getRelayTime } from "./_aqua/demo"; | ||
|
||
async function buttonClick() { | ||
|
@@ -172,7 +152,7 @@ Star (`*`) character can be used as a wildcard to enable logs for multiple compo | |
|
||
### Enabling logs in Node.js | ||
|
||
enable logs, pass the environment variable `DEBUG` with the corresponding log level. For example: | ||
Enable logs by passing the environment variable `DEBUG` with the corresponding log level. For example: | ||
|
||
```sh | ||
DEBUG=fluence:* node --loader ts-node/esm ./src/index.ts | ||
|
@@ -182,13 +162,14 @@ DEBUG=fluence:* node --loader ts-node/esm ./src/index.ts | |
|
||
To enable logs, set the `localStorage.debug` variable. For example: | ||
|
||
``` | ||
localStorage.debug = 'fluence:*' | ||
```javascript | ||
localStorage.debug = "fluence:*"; | ||
``` | ||
|
||
**NOTE** | ||
|
||
In Chromium-based web browsers (e.g. Brave, Chrome, and Electron), the JavaScript console will—by default—only show messages logged by debug if the "Verbose" log level is enabled. | ||
In Chromium-based web browsers (e.g. Brave, Chrome, and Electron), the JavaScript console will be default—only to show | ||
messages logged by debug if the "Verbose" log level is enabled. | ||
|
||
## Development | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"type": "module", | ||
"name": "@fluencelabs/js-client-isomorphic", | ||
"version": "1.0.0", | ||
"description": "Isomorphic entities for js-client", | ||
"files": [ | ||
"dist" | ||
], | ||
"main": "index.js", | ||
"scripts": { | ||
"build": "tsc" | ||
}, | ||
"exports": { | ||
".": "./dist/types.js", | ||
"./fetcher": { | ||
"node": "./dist/fetchers/node.js", | ||
"default": "./dist/fetchers/browser.js" | ||
}, | ||
"./worker-resolver": { | ||
"node": "./dist/worker-resolvers/node.js", | ||
"default": "./dist/worker-resolvers/browser.js" | ||
} | ||
}, | ||
"dependencies": { | ||
"@fluencelabs/avm": "0.52.0", | ||
"@fluencelabs/marine-js": "0.7.2", | ||
"@fluencelabs/marine-worker": "workspace:*", | ||
"threads": "fluencelabs/threads.js#b00a5342380b0278d3ae56dcfb170effb3cad7cd" | ||
shamsartem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
"keywords": [], | ||
"author": "Fluence Labs", | ||
"license": "Apache-2.0" | ||
} |
38 changes: 38 additions & 0 deletions
38
packages/core/js-client-isomorphic/src/fetchers/browser.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* 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 { VersionedPackage } from "../types.js"; | ||
|
||
/** | ||
* @param pkg name of package with version | ||
* @param assetPath path of required asset in given package | ||
* @param root CDN domain in browser or file system root in node | ||
*/ | ||
export async function fetchResource( | ||
pkg: VersionedPackage, | ||
assetPath: string, | ||
root: string, | ||
) { | ||
const refinedAssetPath = assetPath.startsWith("/") | ||
? assetPath.slice(1) | ||
: assetPath; | ||
|
||
const url = new URL(`${pkg.name}@${pkg.version}/` + refinedAssetPath, root); | ||
|
||
return fetch(url).catch(() => { | ||
throw new Error(`Cannot fetch from ${url.toString()}`); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,23 @@ | ||||||
/** | ||||||
* 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 { Worker } from "threads/master"; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
export type VersionedPackage = { name: string; version: string }; | ||||||
export type GetWorker = ( | ||||||
pkg: VersionedPackage, | ||||||
CDNUrl: string, | ||||||
) => Promise<Worker>; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.