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

Commit 7331d48

Browse files
authored
Data as base64 (#2)
1 parent cbecbf9 commit 7331d48

File tree

9 files changed

+54
-26
lines changed

9 files changed

+54
-26
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fluence",
3-
"version": "0.7.106",
3+
"version": "0.7.107",
44
"description": "the browser js-libp2p client for the Fluence network",
55
"main": "./dist/fluence.js",
66
"typings": "./dist/fluence.d.ts",

src/aqua/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @param {string} log_level
1010
* @returns {string}
1111
*/
12-
export function invoke(wasm: any, init_user_id: string, aqua: string, prev_data: string, data: string, log_level: string): string;
12+
export function invoke(wasm: any, init_user_id: string, aqua: string, prev_data: Uint8Array, data: Uint8Array, log_level: string): string;
1313
export function ast(wasm: any, script: string): string;
1414
export function return_current_peer_id(wasm: any, peerId: string, arg0: any): void;
1515
export function return_call_service_result(wasm: any, ret: string, arg0: any): void;

src/aqua/index_bg.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ cachedTextDecoder.decode();
9191
export function getStringFromWasm0(wasm, ptr, len) {
9292
return cachedTextDecoder.decode(getUint8Memory0(wasm).subarray(ptr, ptr + len));
9393
}
94+
95+
function passArray8ToWasm0(wasm, arg, malloc) {
96+
const ptr = malloc(arg.length * 1);
97+
getUint8Memory0(wasm).set(arg, ptr / 1);
98+
WASM_VECTOR_LEN = arg.length;
99+
return ptr;
100+
}
101+
94102
/**
95103
* @param {any} wasm
96104
* @param {string} init_user_id
@@ -106,9 +114,9 @@ export function invoke(wasm, init_user_id, aqua, prev_data, data, log_level) {
106114
var len0 = WASM_VECTOR_LEN;
107115
var ptr1 = passStringToWasm0(wasm, aqua, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
108116
var len1 = WASM_VECTOR_LEN;
109-
var ptr2 = passStringToWasm0(wasm, prev_data, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
117+
var ptr2 = passArray8ToWasm0(wasm, prev_data, wasm.__wbindgen_malloc);
110118
var len2 = WASM_VECTOR_LEN;
111-
var ptr3 = passStringToWasm0(wasm, data, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
119+
var ptr3 = passArray8ToWasm0(wasm, data, wasm.__wbindgen_malloc);
112120
var len3 = WASM_VECTOR_LEN;
113121
var ptr4 = passStringToWasm0(wasm, log_level, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
114122
var len4 = WASM_VECTOR_LEN;

src/fluenceClient.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class FluenceClient {
6666
log.info(`Particle expired. Now: ${now}, ttl: ${particle.ttl}, ts: ${particle.timestamp}`);
6767
} else {
6868
// if there is no subscription yet, previous data is empty
69-
let prevData = [];
69+
let prevData: Uint8Array = Buffer.from([]);
7070
let prevParticle = this.subscriptions.get(particle.id);
7171
if (prevParticle) {
7272
prevData = prevParticle.data;
@@ -79,8 +79,8 @@ export class FluenceClient {
7979
let stepperOutcomeStr = this.interpreter(
8080
particle.init_peer_id,
8181
particle.script,
82-
JSON.stringify(prevData),
83-
JSON.stringify(particle.data),
82+
prevData,
83+
particle.data,
8484
);
8585
let stepperOutcome: StepperOutcome = JSON.parse(stepperOutcomeStr);
8686

@@ -131,7 +131,7 @@ export class FluenceClient {
131131
let _this = this;
132132

133133
return async (particle: Particle) => {
134-
let data = particle.data;
134+
let data: any = particle.data;
135135
let error: any = data['protocol!error'];
136136
if (error !== undefined) {
137137
log.error('error in external particle: ');

src/fluenceConnection.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import pipe from 'it-pipe';
2323
import Multiaddr from 'multiaddr';
2424
import PeerId from 'peer-id';
2525
import * as log from 'loglevel';
26-
import { build, parseParticle, Particle, stringifyParticle } from './particle';
26+
import { build, parseParticle, Particle, toAction } from './particle';
2727

2828
export const PROTOCOL_NAME = '/fluence/faas/1.0.0';
2929

@@ -121,8 +121,9 @@ export class FluenceConnection {
121121
async sendParticle(particle: Particle): Promise<void> {
122122
this.checkConnectedOrThrow();
123123

124-
let particleStr = stringifyParticle(particle);
125-
log.debug('send particle: \n' + JSON.stringify(particle, undefined, 2));
124+
let action = toAction(particle)
125+
let particleStr = JSON.stringify(action);
126+
log.debug('send particle: \n' + JSON.stringify(action, undefined, 2));
126127

127128
// create outgoing substream
128129
const conn = (await this.node.dialProtocol(this.address, PROTOCOL_NAME)) as {

src/particle.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { v4 as uuidv4 } from 'uuid';
18+
import { fromByteArray } from 'base64-js';
1819
import PeerId from 'peer-id';
1920
import { encode } from 'bs58';
2021
import { addData } from './dataStorage';
@@ -29,7 +30,21 @@ export interface Particle {
2930
script: string;
3031
// sign upper fields
3132
signature: string;
32-
data: any;
33+
data: Uint8Array;
34+
}
35+
36+
/**
37+
* Represents particle action to send to a node
38+
*/
39+
interface ParticleAction {
40+
action: 'Particle'
41+
id: string;
42+
init_peer_id: string;
43+
timestamp: number;
44+
ttl: number;
45+
script: string;
46+
signature: number[];
47+
data: string;
3348
}
3449

3550
function wrapScript(selfPeerId: string, script: string, fields: string[]): string {
@@ -63,7 +78,7 @@ export async function build(peerId: PeerId, script: string, data: Map<string, an
6378
ttl: ttl,
6479
script: script,
6580
signature: '',
66-
data: [],
81+
data: Buffer.from([]),
6782
};
6883

6984
particle.signature = await signParticle(peerId, particle);
@@ -72,16 +87,20 @@ export async function build(peerId: PeerId, script: string, data: Map<string, an
7287
}
7388

7489
/**
75-
* Copies a particle and stringify it.
90+
* Creates an action to send to a node.
7691
*/
77-
export function stringifyParticle(call: Particle): string {
78-
let obj: any = { ...call };
79-
obj.action = 'Particle';
80-
81-
// delete it after signatures will be implemented on nodes
82-
obj.signature = [];
83-
84-
return JSON.stringify(obj);
92+
export function toAction(particle: Particle): ParticleAction {
93+
return {
94+
action: "Particle",
95+
id: particle.id,
96+
init_peer_id: particle.init_peer_id,
97+
timestamp: particle.timestamp,
98+
ttl: particle.ttl,
99+
script: particle.script,
100+
// TODO: copy signature from a particle after signatures will be implemented on nodes
101+
signature: [],
102+
data: fromByteArray(particle.data)
103+
};
85104
}
86105

87106
export function parseParticle(str: string): Particle {

src/stepper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import Instance = WebAssembly.Instance;
2626
import Exports = WebAssembly.Exports;
2727
import ExportValue = WebAssembly.ExportValue;
2828

29-
export type InterpreterInvoke = (init_user_id: string, script: string, prev_data: string, data: string) => string;
29+
export type InterpreterInvoke = (init_user_id: string, script: string, prev_data: Uint8Array, data: Uint8Array) => string;
3030
type ImportObject = {
3131
'./aquamarine_client_bg.js': {
3232
// fn call_service_impl(service_id: String, fn_name: String, args: String, security_tetraplets: String) -> String;
@@ -173,7 +173,7 @@ export async function instantiateInterpreter(peerId: PeerId): Promise<Interprete
173173
});
174174
let instance = await interpreterInstance(cfg);
175175

176-
return (init_user_id: string, script: string, prev_data: string, data: string) => {
176+
return (init_user_id: string, script: string, prev_data: Uint8Array, data: Uint8Array) => {
177177
let logLevel = log.getLevel();
178178
let logLevelStr = 'info';
179179
if (logLevel === 0) {

src/stepperOutcome.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616

1717
export interface StepperOutcome {
1818
ret_code: number;
19-
data: number[];
19+
data: Uint8Array;
2020
next_peer_pks: string[];
2121
}

0 commit comments

Comments
 (0)