mirror of
https://github.com/fluencelabs/examples
synced 2025-04-25 10:42:16 +00:00
node example
This commit is contained in:
parent
679d2e7c7c
commit
b4f1f1c052
8
fluence-js-examples/node-example/.prettierrc.js
Normal file
8
fluence-js-examples/node-example/.prettierrc.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
module.exports = {
|
||||||
|
semi: true,
|
||||||
|
trailingComma: 'all',
|
||||||
|
singleQuote: true,
|
||||||
|
printWidth: 120,
|
||||||
|
tabWidth: 4,
|
||||||
|
useTabs: false,
|
||||||
|
};
|
5
fluence-js-examples/node-example/jest.config.js
Normal file
5
fluence-js-examples/node-example/jest.config.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
module.exports = {
|
||||||
|
preset: 'ts-jest',
|
||||||
|
testEnvironment: 'node',
|
||||||
|
testPathIgnorePatterns: ['dist'],
|
||||||
|
};
|
23392
fluence-js-examples/node-example/package-lock.json
generated
23392
fluence-js-examples/node-example/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,24 +1,28 @@
|
|||||||
{
|
{
|
||||||
"name": "node-example",
|
"name": "node-example",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prestart": "npm run compile-aqua",
|
"prestart": "npm run compile-aqua",
|
||||||
"start": "node -r ts-node/register src/index.ts",
|
"start": "node -r ts-node/register src/index.ts",
|
||||||
"compile-aqua": "aqua --import . -i ./aqua/ -o ./src/_aqua",
|
"test": "jest",
|
||||||
"watch-aqua": "chokidar \"**/*.aqua\" -c \"npm run compile-aqua\""
|
"compile-aqua": "aqua --import . -i ./aqua/ -o ./src/_aqua",
|
||||||
},
|
"watch-aqua": "chokidar \"**/*.aqua\" -c \"npm run compile-aqua\""
|
||||||
"author": "",
|
},
|
||||||
"license": "ISC",
|
"author": "",
|
||||||
"devDependencies": {
|
"license": "ISC",
|
||||||
"@fluencelabs/aqua": "^0.6.0-272",
|
"devDependencies": {
|
||||||
"chokidar-cli": "^3.0.0",
|
"@fluencelabs/aqua": "^0.6.0-272",
|
||||||
"ts-node": "^10.2.1",
|
"chokidar-cli": "^3.0.0",
|
||||||
"typescript": "^4.4.2"
|
"ts-node": "^10.2.1",
|
||||||
},
|
"typescript": "^4.4.2",
|
||||||
"dependencies": {
|
"@types/jest": "^27.0.3",
|
||||||
"@fluencelabs/fluence": "^0.19.1",
|
"jest": "^27.4.0",
|
||||||
"@fluencelabs/fluence-network-environment": "^1.0.13"
|
"ts-jest": "^27.0.7"
|
||||||
}
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@fluencelabs/fluence": "^0.19.1",
|
||||||
|
"@fluencelabs/fluence-network-environment": "^1.0.13"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
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 { runServer, waitForKeypressAndStop } from './main';
|
||||||
import { krasnodar } from "@fluencelabs/fluence-network-environment";
|
|
||||||
import { registerCalc, CalcDef } from "./_aqua/calc";
|
|
||||||
|
|
||||||
class Calc implements CalcDef {
|
runServer().then(waitForKeypressAndStop);
|
||||||
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();
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user