mirror of
https://github.com/fluencelabs/examples
synced 2025-06-22 06:11:32 +00:00
feat!: Replace old fluence-js with JS Client (#425)
This commit is contained in:
31
js-client-examples/node-example/.gitignore
vendored
Normal file
31
js-client-examples/node-example/.gitignore
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
/.pnp
|
||||
.pnp.js
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
# production
|
||||
/build
|
||||
|
||||
/dist
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
|
||||
# fluence
|
||||
|
||||
src/_aqua/*
|
||||
.fluence/
|
8
js-client-examples/node-example/.prettierrc.cjs
Normal file
8
js-client-examples/node-example/.prettierrc.cjs
Normal file
@ -0,0 +1,8 @@
|
||||
module.exports = {
|
||||
semi: true,
|
||||
trailingComma: 'all',
|
||||
singleQuote: true,
|
||||
printWidth: 120,
|
||||
tabWidth: 4,
|
||||
useTabs: false,
|
||||
};
|
8
js-client-examples/node-example/aqua/calc.aqua
Normal file
8
js-client-examples/node-example/aqua/calc.aqua
Normal file
@ -0,0 +1,8 @@
|
||||
service Calc("calc"):
|
||||
add(n: f32)
|
||||
subtract(n: f32)
|
||||
multiply(n: f32)
|
||||
divide(n: f32)
|
||||
reset()
|
||||
getResult() -> f32
|
||||
|
13
js-client-examples/node-example/aqua/demo-calculation.aqua
Normal file
13
js-client-examples/node-example/aqua/demo-calculation.aqua
Normal file
@ -0,0 +1,13 @@
|
||||
import "./calc.aqua"
|
||||
|
||||
const PEER ?= "12D3KooWKETqJdR26urWDbkRAVRdPQhcYNSJNGMBTn1zuE9kjQmo"
|
||||
const RELAY ?= "12D3KooWSD5PToNiLQwKDXsu8JSysCwUt8BVUJEqCHcDe7P5h45e"
|
||||
|
||||
func demoCalculation() -> f32:
|
||||
on PEER via RELAY:
|
||||
Calc.add(10)
|
||||
Calc.multiply(5)
|
||||
Calc.subtract(8)
|
||||
Calc.divide(6)
|
||||
res <- Calc.getResult()
|
||||
<- res
|
16
js-client-examples/node-example/jest.config.cjs
Normal file
16
js-client-examples/node-example/jest.config.cjs
Normal file
@ -0,0 +1,16 @@
|
||||
/** @type {import('ts-jest').JestConfigWithTsJest} */
|
||||
module.exports = {
|
||||
extensionsToTreatAsEsm: ['.ts'],
|
||||
moduleNameMapper: {
|
||||
'^(\\.{1,2}/.*)\\.js$': '$1',
|
||||
},
|
||||
testPathIgnorePatterns: ['dist'],
|
||||
transform: {
|
||||
'^.+\\.tsx?$': [
|
||||
'ts-jest',
|
||||
{
|
||||
useESM: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
48855
js-client-examples/node-example/package-lock.json
generated
Normal file
48855
js-client-examples/node-example/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
30
js-client-examples/node-example/package.json
Normal file
30
js-client-examples/node-example/package.json
Normal file
@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "node-example",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"prestart": "npm run compile-aqua",
|
||||
"prebuild": "npm run compile-aqua",
|
||||
"start": "node --loader ts-node/esm ./src/index.ts",
|
||||
"build": "tsc",
|
||||
"test": "jest",
|
||||
"compile-aqua": "fluence aqua -i ./aqua/ -o ./src/_aqua",
|
||||
"watch-aqua": "fluence aqua -w -i ./aqua/ -o ./src/_aqua"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@fluencelabs/cli": "0.2.41",
|
||||
"ts-node": "10.9.1",
|
||||
"@types/jest": "29.4.0",
|
||||
"jest": "29.4.1",
|
||||
"ts-jest": "29.0.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@fluencelabs/js-client.api": "0.11.2",
|
||||
"@fluencelabs/js-client.node": "0.6.4",
|
||||
"@fluencelabs/fluence-network-environment": "1.0.14"
|
||||
}
|
||||
}
|
3
js-client-examples/node-example/run_calculation.sh
Executable file
3
js-client-examples/node-example/run_calculation.sh
Executable file
@ -0,0 +1,3 @@
|
||||
#! /bin/bash
|
||||
|
||||
npx fluence run -i ./aqua/demo-calculation.aqua -f 'demoCalculation()'
|
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);
|
||||
});
|
||||
}
|
18
js-client-examples/node-example/tsconfig.json
Normal file
18
js-client-examples/node-example/tsconfig.json
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"lib": ["es2015", "dom"],
|
||||
"outDir": "./dist/",
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"declaration": true,
|
||||
"declarationMap": false,
|
||||
"sourceMap": true,
|
||||
"moduleResolution": "nodenext"
|
||||
},
|
||||
"exclude": ["node_modules", "dist"],
|
||||
"include": ["src"]
|
||||
}
|
Reference in New Issue
Block a user