Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 34 additions & 33 deletions .github/workflows/js_sdk_ci.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
name: Node.js CI
name: Run tests

defaults:
run:
working-directory: .
run:
working-directory: .

on:
push:
push:

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x, 15.x]

steps:
- uses: actions/checkout@v2

- uses: actions/cache@v2
with:
path: ~/.npm
key: ${{ runner.os }}-v1-node-${{ matrix.node-version }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-v1-node-${{ matrix.node-version }}

- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- run: npm install
- run: npm run test:unit
env:
CI: true


build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x, 15.x]
node-env: ['', ':node']

steps:
- uses: actions/checkout@v2

- uses: actions/cache@v2
with:
path: ~/.npm
key: ${{ runner.os }}-v1-node-${{ matrix.node-version }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-v1-node-${{ matrix.node-version }}

- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Run container with Fluence node
run: docker run -d --rm -e RUST_LOG="info" -p 1210:1210 -p 4310:4310 fluencelabs/fluence -t 1210 -w 4310 -k gKdiCSUr1TFGFEgu2t8Ch1XEUsrN5A2UfBLjSZvfci9SPR3NvZpACfcpPGC3eY4zma1pk7UvYv5zb1VjvPHwCjj --local

- run: npm install
- run: npm run test${{ matrix.node-env }}:all
env:
CI: true
14 changes: 14 additions & 0 deletions jest-patched-jsdom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const Environment = require('jest-environment-jsdom');

module.exports = class CustomTestEnvironment extends Environment {
async setup() {
await super.setup();
if (typeof this.global.TextEncoder === 'undefined') {
const { TextEncoder, TextDecoder } = require('util');
this.global.TextEncoder = TextEncoder;
this.global.TextDecoder = TextDecoder;
this.global.Uint8Array = Uint8Array;
this.global.ArrayBuffer = ArrayBuffer;
}
}
};
6 changes: 3 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
preset: 'ts-jest',
testEnvironment: './jest-patched-jsdom.js',
};
18 changes: 14 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"scripts": {
"test": "jest --env=jsdom --watch",
"test:all": "jest --env=jsdom",
"test:unit": "jest --env=jsdom --testPathPattern=src/__test__/unit",
"test:integration": "jest --env=jsdom --testPathPattern=src/__test__/integration",
"test": "jest --watch",
"test:all": "jest",
"test:unit": "jest --testPathPattern=src/__test__/unit",
"test:integration": "jest --testPathPattern=src/__test__/integration",
"test:node": "jest --env=node --watch",
"test:node:all": "jest --env=node",
"test:node:unit": "jest --env=node --testPathPattern=src/__test__/unit",
"test:node:integration": "jest --env=node --testPathPattern=src/__test__/integration",
"build": "tsc"
},
"repository": "https://github.com/fluencelabs/fluence-js",
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@fluencelabs/aquamarine-interpreter": "0.8.2",
"@fluencelabs/air-interpreter": "0.9.7",
"async": "3.2.0",
"base64-js": "1.3.1",
"bs58": "4.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/__test__/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/*
* start docker container to run integration tests locally:

docker run --rm -e RUST_LOG="info" -p 1210:1210 -p 4310:4310 fluencelabs/fluence -t 1210 -w 4310 -k gKdiCSUr1TFGFEgu2t8Ch1XEUsrN5A2UfBLjSZvfci9SPR3NvZpACfcpPGC3eY4zma1pk7UvYv5zb1VjvPHwCjj
docker run --rm -e RUST_LOG="info" -p 1210:1210 -p 4310:4310 fluencelabs/fluence -t 1210 -w 4310 -k gKdiCSUr1TFGFEgu2t8Ch1XEUsrN5A2UfBLjSZvfci9SPR3NvZpACfcpPGC3eY4zma1pk7UvYv5zb1VjvPHwCjj --local

*/
export const nodes = [
Expand Down
6 changes: 6 additions & 0 deletions src/__test__/integration/legacy.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { nodes } from '../connection';
let client: FluenceClient;

describe('Legacy api suite', () => {
afterEach(async () => {
if (client) {
await client.disconnect();
}
});

it('sendParticle', async () => {
client = await createClient(nodes[0]);

Expand Down
2 changes: 1 addition & 1 deletion src/__test__/unit/air.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('== AIR suite', () => {
await client.initiateFlow(request);

// assert
await expect(error).rejects.toContain("aqua script can't be parsed");
await expect(error).rejects.toContain("air can't be parsed");
});

it('call script without ttl', async function () {
Expand Down
11 changes: 8 additions & 3 deletions src/__test__/unit/ast.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { AquamarineInterpreter } from '../../internal/aqua/interpreter';
import { AirInterpreter } from '@fluencelabs/air-interpreter';
import { genUUID } from '../../internal/particle';

describe('== AST parsing suite', () => {
it('parse simple script and return ast', async function () {
const interpreter = await AquamarineInterpreter.create({} as any);
const interpreter = await AirInterpreter.create(
undefined as any,
undefined as any,
undefined as any,
undefined as any,
);
let ast = interpreter.parseAir(`
(call "node" ("service" "function") [1 2 3] output)
`);

console.log(ast);
ast = JSON.parse(ast);

expect(ast).toEqual({
Expand Down
17 changes: 11 additions & 6 deletions src/internal/ClientImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { AquaCallHandler, errorHandler, fnHandler } from './AquaHandler';
import { loadRelayFn, loadVariablesService } from './RequestFlowBuilder';
import { logParticle, Particle } from './particle';
import log from 'loglevel';
import { AquamarineInterpreter } from './aqua/interpreter';
import { AirInterpreter } from '@fluencelabs/air-interpreter';

const makeDefaultClientHandler = (): AquaCallHandler => {
const res = new AquaCallHandler();
Expand Down Expand Up @@ -54,7 +54,7 @@ export class ClientImpl implements FluenceClient {
}

private connection: FluenceConnection;
private interpreter: AquamarineInterpreter;
private interpreter: AirInterpreter;

constructor(selfPeerIdFull: PeerId) {
this.selfPeerIdFull = selfPeerIdFull;
Expand All @@ -68,13 +68,18 @@ export class ClientImpl implements FluenceClient {
await this.connection.disconnect();
}
this.clearWathcDog();
this.requests.forEach((r) => {
r.cancel();
});
}

async initAquamarineRuntime(): Promise<void> {
this.interpreter = await AquamarineInterpreter.create({
particleHandler: this.interpreterCallback.bind(this),
peerId: this.selfPeerIdFull,
});
this.interpreter = await AirInterpreter.create(
this.interpreterCallback.bind(this),
this.selfPeerId,
'trace',
log.log,
);
}

async connect(multiaddr: string | Multiaddr, options?: FluenceConnectionOptions): Promise<void> {
Expand Down
18 changes: 13 additions & 5 deletions src/internal/RequestFlow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import log, { trace } from 'loglevel';
import PeerId from 'peer-id';
import { AquamarineInterpreter } from './aqua/interpreter';
import { AirInterpreter } from '@fluencelabs/air-interpreter';
import { AquaCallHandler } from './AquaHandler';
import { InterpreterOutcome, PeerIdB58 } from './commonTypes';
import { FluenceConnection } from './FluenceConnection';
Expand All @@ -20,6 +20,7 @@ export class RequestFlow {
private prevData: Uint8Array = Buffer.from([]);
private onTimeoutHandlers = [];
private onErrorHandlers = [];
private timeoutHandle?: NodeJS.Timeout;

readonly id: string;
readonly isExternal: boolean;
Expand All @@ -33,7 +34,7 @@ export class RequestFlow {
const res = new RequestFlow(true, particle.id, particle.script);
res.ttl = particle.ttl;
res.state = particle;
setTimeout(res.raiseTimeout.bind(res), particle.ttl);
res.timeoutHandle = setTimeout(res.raiseTimeout.bind(res), particle.ttl);
return res;
}

Expand All @@ -57,7 +58,7 @@ export class RequestFlow {
this.onErrorHandlers.push(handler);
}

async execute(interpreter: AquamarineInterpreter, connection: FluenceConnection, relayPeerId?: PeerIdB58) {
async execute(interpreter: AirInterpreter, connection: FluenceConnection, relayPeerId?: PeerIdB58) {
if (this.hasExpired()) {
return;
}
Expand Down Expand Up @@ -97,11 +98,18 @@ export class RequestFlow {

if (!connection) {
this.raiseError('Cannot send particle: non connected');
return;
}

this.sendIntoConnection(connection);
}

public cancel() {
if (this.timeoutHandle) {
clearTimeout(this.timeoutHandle);
}
}

private throwIncorrectNextPeerPks(nextPeers: PeerIdB58[]) {
this.raiseError(
`Particle is expected to be sent to only the single peer (relay which client is connected to).
Expand All @@ -127,7 +135,7 @@ relay peer id: ${this.relayPeerId}
};

this.state = particle;
setTimeout(this.raiseTimeout.bind(this), particle.ttl);
this.timeoutHandle = setTimeout(this.raiseTimeout.bind(this), particle.ttl);
}

receiveUpdate(particle: Particle) {
Expand All @@ -145,7 +153,7 @@ relay peer id: ${this.relayPeerId}
}
}

runInterpreter(interpreter: AquamarineInterpreter) {
runInterpreter(interpreter: AirInterpreter) {
const interpreterOutcomeStr = interpreter.invoke(
this.state.init_peer_id,
this.state.script,
Expand Down
Loading