feat!: Expose updated JS Client API via js-client.api package (#246)

This commit is contained in:
Pavel 2023-02-15 03:00:42 +03:00 committed by GitHub
parent 9667c4fec6
commit d4bb8fb429
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 858 additions and 564 deletions

View File

@ -1,4 +1,8 @@
name: Run tests with worflow_call
name: Run tests
defaults:
run:
working-directory: .
on:
workflow_call:
@ -30,83 +34,32 @@ jobs:
name: "Run tests"
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
strategy:
matrix:
node-version:
- 16.x
- 17.x
node-version: [16.x, 17.x, 18.x]
steps:
- name: Import secrets
uses: hashicorp/vault-action@v2.4.3
with:
url: https://vault.fluence.dev
path: jwt/github
role: ci
method: jwt
jwtGithubAudience: "https://github.com/fluencelabs"
jwtTtl: 300
secrets: |
kv/docker-registry/basicauth/ci username | DOCKER_USERNAME ;
kv/docker-registry/basicauth/ci password | DOCKER_PASSWORD
- uses: actions/checkout@v2
- name: Login to DockerHub
uses: docker/login-action@v2
- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
registry: docker.fluence.dev
username: ${{ env.DOCKER_USERNAME }}
password: ${{ env.DOCKER_PASSWORD }}
- name: Checkout
uses: actions/checkout@v3
with:
repository: fluencelabs/fluence-js
ref: ${{ inputs.ref }}
- name: Pull rust-peer image
run: docker pull $RUST_PEER_IMAGE
- name: Run rust-peer
uses: isbang/compose-action@v1.4.1
with:
compose-file: ".github/e2e/docker-compose.yml"
down-flags: "--volumes"
node-version: ${{ matrix.node-version }}
- name: Setup pnpm
uses: pnpm/action-setup@v2.2.4
with:
version: 7
- name: Setup node ${{ matrix.node-version }} with self-hosted registry
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
registry-url: "https://npm.fluence.dev"
cache: "pnpm"
- name: Run container with Fluence node
run: |
docker pull fluencelabs/fluence
docker run -d --rm -e RUST_LOG="info" -p 1210:1210 -p 4310:4310 fluencelabs/fluence -t 1210 -w 4310 -k gKdiCSUr1TFGFEgu2t8Ch1XEUsrN5A2UfBLjSZvfci9SPR3NvZpACfcpPGC3eY4zma1pk7UvYv5zb1VjvPHwCjj --local --aqua-pool-size 2
- run: pnpm i
- name: Set avm version
if: inputs.avm-version != 'null'
uses: fluencelabs/github-actions/npm-set-dependency@main
with:
package: "@fluencelabs/avm"
version: ${{ inputs.avm-version }}
working-directory: packages/fluence-js
package-manager: pnpm
- name: Set marine-js version
if: inputs.marine-js-version != 'null'
uses: fluencelabs/github-actions/npm-set-dependency@main
with:
package: "@fluencelabs/marine-js"
version: ${{ inputs.marine-js-version }}
working-directory: packages/fluence-js
package-manager: pnpm
- run: pnpm -r build
env:
CI: true
- run: pnpm -r test
env:
CI: true

112
.github/workflows/tests.yml.disabled vendored Normal file
View File

@ -0,0 +1,112 @@
name: Run tests with worflow_call
on:
workflow_call:
inputs:
rust-peer-image:
description: "rust-peer image tag"
type: string
default: "fluencelabs/fluence:minimal"
avm-version:
description: "@fluencelabs/avm version"
type: string
default: "null"
marine-js-version:
description: "@fluencelabs/marine-js version"
type: string
default: "null"
ref:
description: "git ref to checkout to"
type: string
default: "master"
env:
RUST_PEER_IMAGE: "${{ inputs.rust-peer-image }}"
FORCE_COLOR: true
CI: true
jobs:
fluence-js:
name: "Run tests"
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
strategy:
matrix:
node-version:
- 16.x
- 17.x
steps:
- name: Import secrets
uses: hashicorp/vault-action@v2.4.3
with:
url: https://vault.fluence.dev
path: jwt/github
role: ci
method: jwt
jwtGithubAudience: "https://github.com/fluencelabs"
jwtTtl: 300
secrets: |
kv/docker-registry/basicauth/ci username | DOCKER_USERNAME ;
kv/docker-registry/basicauth/ci password | DOCKER_PASSWORD
- name: Login to DockerHub
uses: docker/login-action@v2
with:
registry: docker.fluence.dev
username: ${{ env.DOCKER_USERNAME }}
password: ${{ env.DOCKER_PASSWORD }}
- name: Checkout
uses: actions/checkout@v3
with:
repository: fluencelabs/fluence-js
ref: ${{ inputs.ref }}
- name: Pull rust-peer image
run: docker pull $RUST_PEER_IMAGE
- name: Run rust-peer
uses: isbang/compose-action@v1.4.1
with:
compose-file: ".github/e2e/docker-compose.yml"
down-flags: "--volumes"
- name: Setup pnpm
uses: pnpm/action-setup@v2.2.4
with:
version: 7
- name: Setup node ${{ matrix.node-version }} with self-hosted registry
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
registry-url: "https://npm.fluence.dev"
cache: "pnpm"
- run: pnpm i
- name: Set avm version
if: inputs.avm-version != 'null'
uses: fluencelabs/github-actions/npm-set-dependency@main
with:
package: "@fluencelabs/avm"
version: ${{ inputs.avm-version }}
working-directory: packages/fluence-js
package-manager: pnpm
- name: Set marine-js version
if: inputs.marine-js-version != 'null'
uses: fluencelabs/github-actions/npm-set-dependency@main
with:
package: "@fluencelabs/marine-js"
version: ${{ inputs.marine-js-version }}
working-directory: packages/fluence-js
package-manager: pnpm
- run: pnpm -r build
- run: pnpm -r test

View File

@ -13,10 +13,9 @@
},
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {},
"devDependencies": {
"http-server": "14.1.1",
"@types/node": "16.11.59",
"@types/node": "18.13.0",
"ts-node": "10.9.1",
"typescript": "4.7",
"@fluencelabs/aqua-lib": "0.6.0",

View File

@ -17,7 +17,7 @@
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@fluencelabs/fluence": "workspace:0.60.0",
"@fluencelabs/js-client.api": "0.10.0",
"@fluencelabs/fluence-network-environment": "1.0.13",
"base64-js": "1.5.1"
},

View File

@ -6,9 +6,11 @@
* Aqua version: 0.9.4
*
*/
import { FluencePeer } from '@fluencelabs/fluence';
import type { CallParams$$ } from '@fluencelabs/fluence/dist/internal/compilerSupport/v4';
import { callFunction$$, registerService$$ } from '@fluencelabs/fluence/dist/internal/compilerSupport/v4';
// eslint-disable
//ts-nocheck
import type { IFluenceClient as IFluenceClient$$, CallParams as CallParams$$ } from '@fluencelabs/js-client.api';
import { v5_callFunction as callFunction$$, v5_registerService as registerService$$ } from '@fluencelabs/js-client.api';
// Services
@ -17,7 +19,7 @@ import { callFunction$$, registerService$$ } from '@fluencelabs/fluence/dist/int
export type SmokeTestResult = [string | null, string[]];
export function smokeTest(label: string, config?: { ttl?: number }): Promise<SmokeTestResult>;
export function smokeTest(peer: FluencePeer, label: string, config?: { ttl?: number }): Promise<SmokeTestResult>;
export function smokeTest(peer: IFluenceClient$$, label: string, config?: { ttl?: number }): Promise<SmokeTestResult>;
export function smokeTest(...args: any) {
let script = `

View File

@ -1,5 +1,5 @@
import { fromByteArray } from 'base64-js';
import { Fluence } from '@fluencelabs/fluence';
import { Fluence } from '@fluencelabs/js-client.api';
import { krasnodar } from '@fluencelabs/fluence-network-environment';
import { smokeTest } from './_aqua/smoke_test.js';

View File

@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@fluencelabs/fluence": "workspace:0.60.0",
"@fluencelabs/js-client.api": "0.10.0",
"@test/aqua_for_test": "workspace:*",
"@testing-library/jest-dom": "5.16.5",
"@testing-library/react": "13.4.0",

View File

@ -17,7 +17,7 @@
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@fluencelabs/fluence": "workspace:0.60.0",
"@fluencelabs/js-client.api": "0.10.0",
"@fluencelabs/js-client.node": "0.1.0",
"@test/aqua_for_test": "workspace:*"
},

View File

@ -0,0 +1,33 @@
// @ts-check
// If won't use `@ts-check` - just remove that comments (with `@type` JSDoc below).
/** @type import('dts-bundle-generator/config-schema').OutputOptions */
const commonOutputParams = {
inlineDeclareGlobals: false,
sortNodes: true,
};
/** @type import('dts-bundle-generator/config-schema').BundlerConfig */
const config = {
compilationOptions: {
preferredConfigPath: './tsconfig.json',
},
entries: [
{
filePath: './src/index.ts',
outFile: './dist/index.d.ts',
noCheck: false,
libraries: {
importedLibraries: ['node', '@types/node'],
inlinedLibraries: ['@fluencelabs/avm', '@fluencelabs/marine-js'],
},
output: commonOutputParams,
},
],
};
module.exports = config;

View File

@ -1,36 +1,25 @@
{
"_1": "This should actually be named @fluencelabs/js-client.api. Naming it fluence-js is needed for backward compat w/ aqua compiler",
"name": "@fluencelabs/fluence",
"version": "0.60.0",
"name": "@fluencelabs/js-client.api",
"version": "0.10.0",
"description": "JS Client API",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"engines": {
"node": ">=10",
"pnpm": ">=3"
},
"type": "module",
"_2": "dist/internal/ export is needed for backward compat w/ aqua compiler",
"source": "src/index.js",
"main": "dist/index.js",
"umd:main": "dist/index.umd.js",
"module": "dist/index.mjs",
"exports": {
".": {
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"./compilerSupport/v5": {
"import": "./dist/compilerSupport/v5.js",
"types": "./dist/compilerSupport/v5.d.ts"
},
"./dist/compilerSupport/v5": {
"import": "./dist/compilerSupport/v5.js",
"types": "./dist/compilerSupport/v5.d.ts"
},
"./dist/internal/compilerSupport/v4": {
"import": "./dist/compilerSupport/v5.js",
"types": "./dist/compilerSupport/v5.d.ts"
}
"types": "./dist/index.d.ts",
"require": "./dist/index.js",
"default": "./dist/index.modern.mjs"
},
"types": "dist/index.d.ts",
"typings": "dist/index.d.ts",
"scripts": {
"build": "tsc"
"build": "microbundle --generateTypes false src/index.ts && dts-bundle-generator --config dtsbundle.cjs"
},
"repository": "https://github.com/fluencelabs/fluence-js",
"author": "Fluence Labs",
@ -38,5 +27,10 @@
"dependencies": {
"@fluencelabs/interfaces": "0.5.0"
},
"devDependencies": {}
"devDependencies": {
"@fluencelabs/avm": "0.35.4",
"@fluencelabs/marine-js": "0.3.44",
"microbundle": "0.15.1",
"dts-bundle-generator": "7.2.0"
}
}

View File

@ -14,36 +14,12 @@
* limitations under the License.
*/
import type { FnConfig, FunctionCallDef, ServiceDef } from '@fluencelabs/interfaces/compilerSupport';
import type { IFluenceClient } from '@fluencelabs/interfaces/fluenceClient';
import { getArgumentTypes } from '@fluencelabs/interfaces/compilerSupport';
import { isFluencePeer } from '@fluencelabs/interfaces/fluenceClient';
import type { FnConfig, FunctionCallDef, ServiceDef } from '@fluencelabs/interfaces';
import type { IFluenceClient } from '@fluencelabs/interfaces';
import { getArgumentTypes } from '@fluencelabs/interfaces';
import { isFluencePeer } from '@fluencelabs/interfaces';
import { getDefaultPeer } from '../index.js';
export type { IFluenceClient, CallParams } from '@fluencelabs/interfaces/fluenceClient';
export {
ArrayType,
ArrowType,
ArrowWithCallbacks,
ArrowWithoutCallbacks,
BottomType,
FnConfig,
FunctionCallConstants,
FunctionCallDef,
LabeledProductType,
NilType,
NonArrowType,
OptionType,
ProductType,
ScalarNames,
ScalarType,
ServiceDef,
StructType,
TopType,
UnlabeledProductType,
} from '@fluencelabs/interfaces/compilerSupport';
import { getDefaultPeer } from '../util.js';
/**
* Convenience function to support Aqua `func` generation backend
@ -66,7 +42,6 @@ export const callFunction = async (rawFnArgs: Array<any>, def: FunctionCallDef,
/**
* Convenience function to support Aqua `service` generation backend
* The compiler only need to generate a call the function and provide the corresponding definitions and the air script
*
* @param args - raw arguments passed by user to the generated function
* @param def - service definition generated by the Aqua compiler
*/

View File

@ -1,41 +0,0 @@
/*
* Copyright 2022 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { IFluenceClient } from './implementation.js';
export { CallParams as CallParams$$ } from './implementation.js';
export {
ArrayType as ArrayType$$,
ArrowType as ArrowType$$,
ArrowWithCallbacks as ArrowWithCallbacks$$,
ArrowWithoutCallbacks as ArrowWithoutCallbacks$$,
BottomType as BottomType$$,
FnConfig as FnConfig$$,
FunctionCallConstants as FunctionCallConstants$$,
FunctionCallDef as FunctionCallDef$$,
LabeledProductType as LabeledProductType$$,
NilType as NilType$$,
NonArrowType as NonArrowType$$,
OptionType as OptionType$$,
ProductType as ProductType$$,
ScalarNames as ScalarNames$$,
ScalarType as ScalarType$$,
ServiceDef as ServiceDef$$,
StructType as StructType$$,
TopType as TopType$$,
UnlabeledProductType as UnlabeledProductType$$,
callFunction as callFunction$$,
registerService as registerService$$,
} from './implementation.js';

View File

@ -1,40 +1,33 @@
import type { IFluenceClient, ClientOptions } from '@fluencelabs/interfaces/fluenceClient';
import { getDefaultPeer } from './util.js';
import type { IFluenceClient, ClientOptions } from '@fluencelabs/interfaces';
export type { IFluenceClient, ClientOptions, CallParams } from '@fluencelabs/interfaces';
export { IFluenceClient, ClientOptions, CallParams } from '@fluencelabs/interfaces/fluenceClient';
export {
ArrayType,
ArrowType,
ArrowWithCallbacks,
ArrowWithoutCallbacks,
BottomType,
FnConfig,
FunctionCallConstants,
FunctionCallDef,
LabeledProductType,
NilType,
NonArrowType,
OptionType,
ProductType,
ScalarNames,
ScalarType,
ServiceDef,
StructType,
TopType,
UnlabeledProductType,
} from '@fluencelabs/interfaces';
// TODO: hack needed to kinda have backward compat with compiler api
export type FluencePeer = IFluenceClient;
const getPeerFromGlobalThis = (): IFluenceClient | undefined => {
// @ts-ignore
return globalThis.defaultPeer;
};
// TODO: DXJ-271
const REJECT_MESSAGE = 'You probably forgot to add script tag. Read about it here: ';
/**
* Wait until the js client script it loaded and return the default peer from globalThis
*/
export const getDefaultPeer = (): Promise<IFluenceClient> => {
return new Promise((resolve, reject) => {
let interval: NodeJS.Timer | undefined;
let hits = 50;
interval = setInterval(() => {
if (hits === 0) {
clearInterval(interval);
reject(REJECT_MESSAGE);
}
let res = getPeerFromGlobalThis();
if (res) {
clearInterval(interval);
resolve(res);
}
hits--;
}, 100);
});
};
export {
callFunction as v5_callFunction,
registerService as v5_registerService,
} from './compilerSupport/implementation.js';
/**
* Public interface to Fluence JS

View File

@ -0,0 +1,41 @@
import type { IFluenceClient } from '@fluencelabs/interfaces';
const getPeerFromGlobalThis = (): IFluenceClient | undefined => {
// @ts-ignore
return globalThis.defaultPeer;
};
// TODO: DXJ-271
const REJECT_MESSAGE = 'You probably forgot to add script tag. Read about it here: ';
// Let's assume that if the library has not been loaded in 5 seconds, then the user has forgotten to add the script tag
const POLL_PEER_TIMEOUT = 5000;
// The script might be cached so need to try loading it ASAP, thus short interval
const POLL_PEER_INTERVAL = 100;
/**
* Wait until the js client script it loaded and return the default peer from globalThis
*/
export const getDefaultPeer = (): Promise<IFluenceClient> => {
return new Promise((resolve, reject) => {
// This function is internal
// Make it sure that would be zero way for unnecessary types
// to break out into the public API
let interval: any;
let hits = POLL_PEER_TIMEOUT / POLL_PEER_INTERVAL;
interval = setInterval(() => {
if (hits === 0) {
clearInterval(interval);
reject(REJECT_MESSAGE);
}
let res = getPeerFromGlobalThis();
if (res) {
clearInterval(interval);
resolve(res);
}
hits--;
}, POLL_PEER_INTERVAL);
});
};

View File

@ -1,7 +1,9 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
"outDir": "./dist",
"skipLibCheck": false
},
"include": ["./src/**/*"],
"exclude": ["node_modules", "dist"]
}

View File

@ -22,7 +22,7 @@
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@fluencelabs/js-peer": "0.5.0",
"@fluencelabs/js-peer": "0.6.0",
"@fluencelabs/avm": "0.35.3",
"@fluencelabs/marine-js": "0.3.42",
"platform": "1.3.6"

View File

@ -1,22 +1 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
bundle/
tmp/
dist
esm
types
# Dependency directories
node_modules/
jspm_packages/
.idea

View File

@ -71,6 +71,7 @@ const buildClient = async () => {
});
workerConfig.build!.rollupOptions = {
plugins: [
// @ts-ignore
inject.default({
Buffer: ['buffer', 'Buffer'],
process: 'process',

View File

@ -1,6 +1,6 @@
{
"name": "@fluencelabs/js-client.web.standalone",
"version": "0.1.0",
"version": "0.11.0",
"description": "TypeScript implementation of Fluence Peer",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
@ -16,7 +16,7 @@
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@fluencelabs/js-peer": "0.5.0",
"@fluencelabs/js-peer": "0.6.0",
"buffer": "6.0.3",
"process": "0.11.10"
},

View File

@ -15,7 +15,7 @@
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@fluencelabs/js-peer": "workspace:0.60.0"
"@fluencelabs/js-peer": "0.6.0"
},
"devDependencies": {
"@types/node": "16.11.59",

View File

@ -17,6 +17,14 @@
"./compilerSupport": {
"import": "./dist/compilerSupport.js",
"types": "./dist/compilerSupport.d.ts"
},
"./dist/fluenceClient": {
"import": "./dist/fluenceClient.js",
"types": "./dist/fluenceClient.d.ts"
},
"./dist/compilerSupport": {
"import": "./dist/compilerSupport.js",
"types": "./dist/compilerSupport.d.ts"
}
},
"typesVersions": {

View File

@ -1,7 +1,7 @@
import { SecurityTetraplet } from '@fluencelabs/avm';
import type { SecurityTetraplet } from '@fluencelabs/avm';
import type { LogLevel } from '@fluencelabs/marine-js/dist/types';
import type { MultiaddrInput } from '@multiformats/multiaddr';
import { FnConfig, FunctionCallDef, ServiceDef } from './compilerSupport.js';
// import type { MultiaddrInput } from '@multiformats/multiaddr';
import type { FnConfig, FunctionCallDef, ServiceDef } from './compilerSupport.js';
/**
* Peer ID's id as a base58 string (multihash/CIDv0).
@ -52,7 +52,8 @@ type Node = {
multiaddr: string;
};
export type RelayOptions = string | MultiaddrInput | Node;
// TODO: either drop support for this exact type or get it back
export type RelayOptions = string | /* MultiaddrInput | */ Node;
export type KeyTypes = 'RSA' | 'Ed25519' | 'secp256k1';

View File

@ -1,6 +1,6 @@
{
"name": "@fluencelabs/js-peer",
"version": "0.5.0",
"version": "0.6.0",
"description": "TypeScript implementation of Fluence Peer",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",

876
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff