mirror of
https://github.com/fluencelabs/examples
synced 2025-06-16 19:41:21 +00:00
node example
This commit is contained in:
12
fluence-js-examples/node-example/src/__test__/test.spec.ts
Normal file
12
fluence-js-examples/node-example/src/__test__/test.spec.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { runServer } from '../main';
|
||||
import { demoCalculation } from '../_aqua/demo-calculation';
|
||||
|
||||
describe('smoke test', () => {
|
||||
it('should work', async () => {
|
||||
await runServer();
|
||||
|
||||
const res = await demoCalculation();
|
||||
|
||||
expect(res).toBe(7);
|
||||
});
|
||||
});
|
@ -1,64 +1,3 @@
|
||||
import { Fluence, KeyPair } from "@fluencelabs/fluence";
|
||||
import { krasnodar } from "@fluencelabs/fluence-network-environment";
|
||||
import { registerCalc, CalcDef } from "./_aqua/calc";
|
||||
import { runServer, waitForKeypressAndStop } from './main';
|
||||
|
||||
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 keypress = async () => {
|
||||
process.stdin.setRawMode(true);
|
||||
return new Promise<void>((resolve) =>
|
||||
process.stdin.once("data", () => {
|
||||
process.stdin.setRawMode(false);
|
||||
resolve();
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
const relay = krasnodar[0];
|
||||
// generated with `npx aqua create_keypair`
|
||||
const skBytes = "tOpsT08fvYMnRypj3VtSoqWMN5W/AptKsP39yanlkg4=";
|
||||
|
||||
async function main() {
|
||||
await Fluence.start({
|
||||
connectTo: relay,
|
||||
KeyPair: await KeyPair.fromEd25519SK(Buffer.from(skBytes, "base64")),
|
||||
});
|
||||
|
||||
registerCalc(new Calc());
|
||||
|
||||
console.log("application started");
|
||||
console.log("peer id is: ", Fluence.getStatus().peerId);
|
||||
console.log("relay is: ", Fluence.getStatus().relayPeerId);
|
||||
console.log("press any key to continue");
|
||||
await keypress();
|
||||
|
||||
await Fluence.stop();
|
||||
}
|
||||
|
||||
main();
|
||||
runServer().then(waitForKeypressAndStop);
|
||||
|
58
fluence-js-examples/node-example/src/main.ts
Normal file
58
fluence-js-examples/node-example/src/main.ts
Normal file
@ -0,0 +1,58 @@
|
||||
import { Fluence, KeyPair } from '@fluencelabs/fluence';
|
||||
import { krasnodar } from '@fluencelabs/fluence-network-environment';
|
||||
import { registerCalc, CalcDef } from './_aqua/calc';
|
||||
|
||||
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 = krasnodar[0];
|
||||
// generated with `npx aqua create_keypair`
|
||||
const skBytes = 'tOpsT08fvYMnRypj3VtSoqWMN5W/AptKsP39yanlkg4=';
|
||||
|
||||
export async function runServer() {
|
||||
await Fluence.start({
|
||||
connectTo: relay,
|
||||
KeyPair: await KeyPair.fromEd25519SK(Buffer.from(skBytes, 'base64')),
|
||||
});
|
||||
|
||||
registerCalc(new Calc());
|
||||
|
||||
console.log('application started');
|
||||
console.log('peer id is: ', Fluence.getStatus().peerId);
|
||||
console.log('relay is: ', Fluence.getStatus().relayPeerId);
|
||||
console.log('press any key to quit...');
|
||||
}
|
||||
|
||||
export async function waitForKeypressAndStop() {
|
||||
process.stdin.setRawMode(true);
|
||||
process.stdin.resume();
|
||||
process.stdin.on('data', async () => {
|
||||
await Fluence.stop();
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user