Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit 44eb149

Browse files
author
Akim
authored
feat(js-client): Update libp2p ecosystem [fixes DXJ-551] (#393)
* Update libp2p ecosystem
1 parent bf0ed95 commit 44eb149

File tree

9 files changed

+358
-453
lines changed

9 files changed

+358
-453
lines changed

.github/workflows/e2e.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ jobs:
4343
uses: fluencelabs/aqua/.github/workflows/tests.yml@main
4444
with:
4545
js-client-snapshots: "${{ needs.js-client.outputs.js-client-snapshots }}"
46-
nox-image: "fluencelabs/nox:0.4.2"
46+
nox-image: "fluencelabs/nox:unstable"
4747
flox:
4848
needs:
4949
- js-client
5050

5151
uses: fluencelabs/flox/.github/workflows/tests.yml@main
5252
with:
5353
js-client-snapshots: "${{ needs.js-client.outputs.js-client-snapshots }}"
54-
nox-image: "fluencelabs/nox:unstable_minimal"
54+
nox-image: "fluencelabs/nox:unstable"

packages/core/js-client/package.json

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,28 @@
3030
"author": "Fluence Labs",
3131
"license": "Apache-2.0",
3232
"dependencies": {
33-
"@chainsafe/libp2p-noise": "13.0.0",
34-
"@chainsafe/libp2p-yamux": "5.0.0",
33+
"@chainsafe/libp2p-noise": "14.0.0",
34+
"@chainsafe/libp2p-yamux": "6.0.1",
3535
"@fluencelabs/avm": "0.54.0",
3636
"@fluencelabs/interfaces": "workspace:*",
3737
"@fluencelabs/js-client-isomorphic": "workspace:*",
3838
"@fluencelabs/marine-worker": "0.5.0",
3939
"@fluencelabs/threads": "^2.0.0",
40-
"@libp2p/crypto": "2.0.3",
41-
"@libp2p/interface": "0.1.2",
42-
"@libp2p/peer-id": "3.0.2",
43-
"@libp2p/peer-id-factory": "3.0.3",
44-
"@libp2p/websockets": "7.0.4",
40+
"@libp2p/crypto": "3.0.1",
41+
"@libp2p/identify": "1.0.4",
42+
"@libp2p/interface": "1.0.1",
43+
"@libp2p/peer-id": "4.0.1",
44+
"@libp2p/peer-id-factory": "4.0.0",
45+
"@libp2p/ping": "1.0.4",
46+
"@libp2p/websockets": "8.0.5",
4547
"@multiformats/multiaddr": "11.3.0",
4648
"bs58": "5.0.0",
47-
"buffer": "6.0.3",
4849
"debug": "4.3.4",
49-
"it-length-prefixed": "8.0.4",
50-
"it-map": "2.0.0",
51-
"it-pipe": "2.0.5",
50+
"it-length-prefixed": "9.0.3",
51+
"it-map": "3.0.5",
52+
"it-pipe": "3.0.1",
5253
"js-base64": "3.7.5",
53-
"libp2p": "0.46.6",
54+
"libp2p": "1.0.7",
5455
"multiformats": "11.0.1",
5556
"rxjs": "7.5.5",
5657
"uint8arrays": "4.0.3",
@@ -59,7 +60,7 @@
5960
},
6061
"devDependencies": {
6162
"@fluencelabs/aqua-api": "0.13.0",
62-
"@rollup/plugin-inject": "5.0.3",
63+
"@rollup/plugin-inject": "5.0.5",
6364
"@types/bs58": "4.0.1",
6465
"@types/debug": "4.1.7",
6566
"@types/node": "20.7.0",

packages/core/js-client/src/connection/RelayConnection.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@
1717
import { noise } from "@chainsafe/libp2p-noise";
1818
import { yamux } from "@chainsafe/libp2p-yamux";
1919
import { PeerIdB58 } from "@fluencelabs/interfaces";
20-
import type { Stream } from "@libp2p/interface/connection";
21-
import type { PeerId } from "@libp2p/interface/peer-id";
20+
import { identify } from "@libp2p/identify";
21+
import type { PeerId, Stream } from "@libp2p/interface";
2222
import { peerIdFromString } from "@libp2p/peer-id";
23+
import { ping } from "@libp2p/ping";
2324
import { webSockets } from "@libp2p/websockets";
2425
import { all } from "@libp2p/websockets/filters";
2526
import { multiaddr, type Multiaddr } from "@multiformats/multiaddr";
2627
import { decode, encode } from "it-length-prefixed";
2728
import map from "it-map";
2829
import { pipe } from "it-pipe";
2930
import { createLibp2p, Libp2p } from "libp2p";
30-
import { identifyService } from "libp2p/identify";
31-
import { pingService } from "libp2p/ping";
3231
import { Subject } from "rxjs";
3332
import { fromString } from "uint8arrays/from-string";
3433
import { toString } from "uint8arrays/to-string";
@@ -139,8 +138,8 @@ export class RelayConnection implements IConnection {
139138
},
140139
},
141140
services: {
142-
identify: identifyService(),
143-
ping: pingService(),
141+
identify: identify(),
142+
ping: ping(),
144143
},
145144
});
146145

@@ -192,7 +191,7 @@ export class RelayConnection implements IConnection {
192191
log.trace("created stream with id ", stream.id);
193192
const sink = stream.sink;
194193

195-
await pipe([fromString(serializeToString(particle))], encode(), sink);
194+
await pipe([fromString(serializeToString(particle))], encode, sink);
196195
log.trace("data written to sink");
197196
}
198197

@@ -260,7 +259,7 @@ export class RelayConnection implements IConnection {
260259
({ stream }) => {
261260
void pipe(
262261
stream.source,
263-
decode(),
262+
decode,
264263
(source) => {
265264
return map(source, (buf) => {
266265
return toString(buf.subarray());

packages/core/js-client/src/jsPeer/FluencePeer.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Buffer } from "buffer";
18-
1917
import {
2018
deserializeAvmResult,
2119
InterpreterResult,
@@ -165,7 +163,7 @@ export abstract class FluencePeer {
165163
* @param serviceId - the service id by which the service can be accessed in aqua
166164
*/
167165
async registerMarineService(
168-
wasm: SharedArrayBuffer | Buffer,
166+
wasm: ArrayBuffer | SharedArrayBuffer,
169167
serviceId: string,
170168
): Promise<void> {
171169
if (this.jsServiceHost.hasService(serviceId)) {
@@ -342,10 +340,7 @@ export abstract class FluencePeer {
342340
private async sendParticleToRelay(
343341
item: ParticleQueueItem & { result: InterpreterResult },
344342
) {
345-
const newParticle = cloneWithNewData(
346-
item.particle,
347-
Buffer.from(item.result.data),
348-
);
343+
const newParticle = cloneWithNewData(item.particle, item.result.data);
349344

350345
log_particle.debug(
351346
"id %s. sending particle into network. Next peer ids: %s",
@@ -407,7 +402,10 @@ export abstract class FluencePeer {
407402
retCode: res.retCode,
408403
};
409404

410-
const newParticle = cloneWithNewData(item.particle, Buffer.from([]));
405+
const newParticle = cloneWithNewData(
406+
item.particle,
407+
Uint8Array.from([]),
408+
);
411409

412410
this._incomingParticles.next({
413411
...item,
@@ -467,7 +465,7 @@ export abstract class FluencePeer {
467465
private mapParticleGroup(
468466
group$: GroupedObservable<string, ParticleQueueItem>,
469467
) {
470-
let prevData: Uint8Array = Buffer.from([]);
468+
let prevData = Uint8Array.from([]);
471469

472470
return group$.pipe(
473471
concatMap(async (item) => {
@@ -515,13 +513,14 @@ export abstract class FluencePeer {
515513
const res = await this.marineHost.callService("avm", "invoke", args);
516514

517515
avmCallResult = deserializeAvmResult(res);
516+
// TODO: This is bug in @fluencelabs/avm package. 'avmCallResult.data' actually number array, not Uint8Array as stated in type.
517+
avmCallResult.data = Uint8Array.from(avmCallResult.data);
518518
} catch (e) {
519519
avmCallResult = e instanceof Error ? e : new Error(String(e));
520520
}
521521

522522
if (!(avmCallResult instanceof Error) && avmCallResult.retCode === 0) {
523-
const newData = Buffer.from(avmCallResult.data);
524-
prevData = newData;
523+
prevData = avmCallResult.data;
525524
}
526525

527526
return {

packages/core/js-client/src/keypair/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ import {
1919
generateKeyPair,
2020
unmarshalPublicKey,
2121
} from "@libp2p/crypto/keys";
22-
import type { PrivateKey, PublicKey } from "@libp2p/interface/keys";
23-
import type { PeerId } from "@libp2p/interface/peer-id";
22+
import type { PrivateKey, PublicKey, PeerId } from "@libp2p/interface";
2423
import { createFromPrivKey } from "@libp2p/peer-id-factory";
2524
import bs58 from "bs58";
2625
import { toUint8Array } from "js-base64";
@@ -91,11 +90,11 @@ export class KeyPair {
9190
return this.privateKey.marshal().subarray(0, 32);
9291
}
9392

94-
signBytes(data: Uint8Array): Promise<Uint8Array> {
93+
async signBytes(data: Uint8Array): Promise<Uint8Array> {
9594
return this.privateKey.sign(data);
9695
}
9796

98-
verify(data: Uint8Array, signature: Uint8Array): Promise<boolean> {
97+
async verify(data: Uint8Array, signature: Uint8Array): Promise<boolean> {
9998
return this.publicKey.verify(data, signature);
10099
}
101100
}

packages/core/js-client/src/services/SingleModuleSrv.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Buffer } from "buffer";
18-
1917
import { v4 as uuidv4 } from "uuid";
2018

2119
import { ServiceFnArgs } from "../compilerSupport/types.js";
@@ -53,12 +51,17 @@ export class Srv {
5351

5452
try {
5553
const newServiceId = uuidv4();
56-
const buffer = Buffer.from(wasmContent, "base64");
54+
55+
const wasmContentBinary = Uint8Array.from(atob(wasmContent), (m) => {
56+
// codePointAt cannot return `undefined` value here as callback is called on every symbol
57+
return m.codePointAt(0) ?? 0;
58+
});
59+
5760
// TODO:: figure out why SharedArrayBuffer is not working here
5861
// const sab = new SharedArrayBuffer(buffer.length);
5962
// const tmp = new Uint8Array(sab);
6063
// tmp.set(buffer, 0);
61-
await this.peer.registerMarineService(buffer, newServiceId);
64+
await this.peer.registerMarineService(wasmContentBinary, newServiceId);
6265
this.services.add(newServiceId);
6366

6467
return {

packages/core/js-client/src/services/builtins.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Buffer } from "buffer";
18-
1917
import { JSONValue } from "@fluencelabs/interfaces";
2018
import bs58 from "bs58";
2119
import { sha256 } from "multiformats/hashes/sha2";
@@ -332,7 +330,7 @@ export const builtInServices: Record<
332330
}),
333331

334332
sha256_string: withSchema(z.tuple([z.string()]))(async ([input]) => {
335-
const inBuffer = Buffer.from(input);
333+
const inBuffer = new TextEncoder().encode(input);
336334
const multihash = await sha256.digest(inBuffer);
337335

338336
return success(bs58.encode(multihash.bytes));

packages/core/js-client/vite.config.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* Copyright 2023 Fluence Labs Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,11 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
import inject from "@rollup/plugin-inject";
18-
import tsconfigPaths from "vite-tsconfig-paths";
1917
import { createRequire } from "module";
20-
import { PluginOption, UserConfig } from "vite";
18+
19+
import inject from "@rollup/plugin-inject";
2120
import { transform } from "esbuild";
21+
import { PluginOption, UserConfig } from "vite";
22+
import tsconfigPaths from "vite-tsconfig-paths";
2223

2324
const require = createRequire(import.meta.url);
2425
const esbuildShim = require.resolve("node-stdlib-browser/helpers/esbuild/shim");
@@ -35,6 +36,7 @@ function minifyEs(): PluginOption {
3536
) {
3637
return await transform(code, { minify: true });
3738
}
39+
3840
return code;
3941
},
4042
},
@@ -48,21 +50,19 @@ const config: UserConfig = {
4850
lib: {
4951
entry: "./src/index.ts",
5052
name: "js-client",
51-
fileName: () => "index.min.js",
53+
fileName: () => {
54+
return "index.min.js";
55+
},
5256
formats: ["es"],
5357
},
5458
outDir: "./dist/browser",
5559
rollupOptions: {
5660
plugins: [
57-
{
58-
// @ts-ignore
59-
...inject({
60-
global: [esbuildShim, "global"],
61-
process: [esbuildShim, "process"],
62-
Buffer: [esbuildShim, "Buffer"],
63-
}),
64-
enforce: "post",
65-
},
61+
inject({
62+
global: [esbuildShim, "global"],
63+
process: [esbuildShim, "process"],
64+
Buffer: [esbuildShim, "Buffer"],
65+
}),
6666
],
6767
},
6868
},

0 commit comments

Comments
 (0)