mirror of
https://github.com/fluencelabs/examples
synced 2025-06-17 03:51:21 +00:00
feat!: Replace old fluence-js with JS Client (#425)
This commit is contained in:
16
js-client-examples/node-example/src/__test__/test.spec.ts
Normal file
16
js-client-examples/node-example/src/__test__/test.spec.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { justStop, runServer } from '../main.js';
|
||||
import { demoCalculation } from '../_aqua/demo-calculation.js';
|
||||
|
||||
describe('smoke test', () => {
|
||||
it('should work', async () => {
|
||||
try {
|
||||
await runServer();
|
||||
|
||||
const res = await demoCalculation();
|
||||
|
||||
expect(res).toBe(7);
|
||||
} finally {
|
||||
await justStop();
|
||||
}
|
||||
}, 15000);
|
||||
});
|
5
js-client-examples/node-example/src/index.ts
Normal file
5
js-client-examples/node-example/src/index.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { runServer, waitForKeypressAndStop } from './main.js';
|
||||
|
||||
runServer()
|
||||
.then(waitForKeypressAndStop)
|
||||
.catch((e) => console.error('error: ', e));
|
70
js-client-examples/node-example/src/main.ts
Normal file
70
js-client-examples/node-example/src/main.ts
Normal file
@ -0,0 +1,70 @@
|
||||
import '@fluencelabs/js-client.node';
|
||||
import { Fluence } from '@fluencelabs/js-client.api';
|
||||
import { kras } from '@fluencelabs/fluence-network-environment';
|
||||
import { registerCalc, CalcDef } from './_aqua/calc.js';
|
||||
|
||||
class Calc implements CalcDef {
|
||||
private _state: number = 0;
|
||||
|
||||
add(n: number) {
|
||||
this._state += n;
|
||||
}
|
||||
|
||||
subtract(n: number) {
|
||||
this._state -= n;
|
||||
}
|
||||
|
||||
multiply(n: number) {
|
||||
this._state *= n;
|
||||
}
|
||||
|
||||
divide(n: number) {
|
||||
this._state /= n;
|
||||
}
|
||||
|
||||
reset() {
|
||||
this._state = 0;
|
||||
}
|
||||
|
||||
getResult() {
|
||||
return this._state;
|
||||
}
|
||||
}
|
||||
|
||||
const relay = kras[0];
|
||||
|
||||
// generated with `npx aqua create_keypair`
|
||||
const skBase64 = 'tOpsT08fvYMnRypj3VtSoqWMN5W/AptKsP39yanlkg4=';
|
||||
|
||||
export async function runServer() {
|
||||
const skBytes = Buffer.from(skBase64, 'base64');
|
||||
|
||||
await Fluence.connect(relay, {
|
||||
keyPair: {
|
||||
type: 'Ed25519',
|
||||
source: Uint8Array.from(skBytes),
|
||||
},
|
||||
});
|
||||
|
||||
registerCalc(new Calc());
|
||||
|
||||
const client = await Fluence.getClient();
|
||||
|
||||
console.log('application started');
|
||||
console.log('peer id is: ', client.getPeerId());
|
||||
console.log('relay is: ', client.getRelayPeerId());
|
||||
console.log('press any key to quit...');
|
||||
}
|
||||
|
||||
export async function justStop() {
|
||||
await Fluence.disconnect();
|
||||
}
|
||||
|
||||
export async function waitForKeypressAndStop() {
|
||||
process.stdin.setRawMode(true);
|
||||
process.stdin.resume();
|
||||
process.stdin.on('data', async () => {
|
||||
await Fluence.disconnect();
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user