mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-19 11:01:21 +00:00
Added the js-client api package, WIP
This commit is contained in:
22
packages/client/api/.gitignore
vendored
Normal file
22
packages/client/api/.gitignore
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
# 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/
|
||||
tmp/
|
||||
|
||||
dist
|
||||
esm
|
||||
types
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
.idea
|
11
packages/client/api/README.md
Normal file
11
packages/client/api/README.md
Normal file
@ -0,0 +1,11 @@
|
||||
# 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)
|
22
packages/client/api/package.json
Normal file
22
packages/client/api/package.json
Normal file
@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "@fluencelabs/js-client.web.standalone",
|
||||
"version": "0.1.0",
|
||||
"description": "TypeScript implementation of Fluence Peer",
|
||||
"main": "./dist/index.js",
|
||||
"typings": "./dist/index.d.ts",
|
||||
"engines": {
|
||||
"node": ">=10",
|
||||
"pnpm": ">=3"
|
||||
},
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "node --loader ts-node/esm ./build.ts"
|
||||
},
|
||||
"repository": "https://github.com/fluencelabs/fluence-js",
|
||||
"author": "Fluence Labs",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@fluencelabs/js-peer": "workspace:*"
|
||||
},
|
||||
"devDependencies": {}
|
||||
}
|
75
packages/client/api/src/index.ts
Normal file
75
packages/client/api/src/index.ts
Normal file
@ -0,0 +1,75 @@
|
||||
import type { IFluencePeer } from '@fluencelabs/js-peer/dist/interfaces/index.js';
|
||||
import type { PeerConfig } from '@fluencelabs/js-peer/dist/interfaces/peerConfig';
|
||||
|
||||
const getPeerFromGlobalThis = (): IFluencePeer | undefined => {
|
||||
// @ts-ignore
|
||||
return globalThis.defaultPeer;
|
||||
};
|
||||
|
||||
const REJECT_MESSAGE = "Couldn't load the peer. Please try this and this or refer to the docs bla bla";
|
||||
|
||||
/**
|
||||
* Wait until the js client script it loaded and return the default peer from globalThis
|
||||
* @returns
|
||||
*/
|
||||
const getDefaultPeer = (): Promise<IFluencePeer> => {
|
||||
// @ts-ignore
|
||||
return new Promise((resolve, reject) => {
|
||||
let interval: any;
|
||||
let hits = 20;
|
||||
interval = setInterval(() => {
|
||||
if (hits === 0) {
|
||||
clearInterval(interval);
|
||||
reject(REJECT_MESSAGE);
|
||||
}
|
||||
|
||||
let res = getPeerFromGlobalThis();
|
||||
if (res) {
|
||||
clearInterval(interval);
|
||||
resolve(res);
|
||||
}
|
||||
hits--;
|
||||
}, 100);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Public interface to Fluence JS
|
||||
*/
|
||||
export const Fluence = {
|
||||
/**
|
||||
* Initializes the default peer: starts the Aqua VM, initializes the default call service handlers
|
||||
* and (optionally) connect to the Fluence network
|
||||
* @param config - object specifying peer configuration
|
||||
*/
|
||||
start: async (config?: PeerConfig): Promise<void> => {
|
||||
const peer = await getDefaultPeer();
|
||||
return peer.start(config);
|
||||
},
|
||||
|
||||
/**
|
||||
* Un-initializes the default peer: stops all the underlying workflows, stops the Aqua VM
|
||||
* and disconnects from the Fluence network
|
||||
*/
|
||||
stop: async (): Promise<void> => {
|
||||
const peer = await getDefaultPeer();
|
||||
return peer.stop();
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the default peer's status
|
||||
* @returns Default peer's status
|
||||
*/
|
||||
// getStatus: async () => {
|
||||
// const peer = await getDefaultPeer();
|
||||
// return peer.getStatus();
|
||||
// },
|
||||
|
||||
/**
|
||||
* Get the default peer instance
|
||||
* @returns the default peer instance
|
||||
*/
|
||||
getPeer: async (): Promise<IFluencePeer> => {
|
||||
return getDefaultPeer();
|
||||
},
|
||||
};
|
8
packages/client/api/tsconfig.json
Normal file
8
packages/client/api/tsconfig.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "../../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"moduleResolution": "node"
|
||||
},
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
Reference in New Issue
Block a user