Data as base64 (#2)

This commit is contained in:
Dima
2020-12-24 19:11:10 +03:00
committed by GitHub
parent cbecbf95f3
commit 7331d48afd
9 changed files with 54 additions and 26 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "fluence",
"version": "0.7.103",
"version": "0.7.106",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -1,6 +1,6 @@
{
"name": "fluence",
"version": "0.7.106",
"version": "0.7.107",
"description": "the browser js-libp2p client for the Fluence network",
"main": "./dist/fluence.js",
"typings": "./dist/fluence.d.ts",

2
src/aqua/index.d.ts vendored
View File

@ -9,7 +9,7 @@
* @param {string} log_level
* @returns {string}
*/
export function invoke(wasm: any, init_user_id: string, aqua: string, prev_data: string, data: string, log_level: string): string;
export function invoke(wasm: any, init_user_id: string, aqua: string, prev_data: Uint8Array, data: Uint8Array, log_level: string): string;
export function ast(wasm: any, script: string): string;
export function return_current_peer_id(wasm: any, peerId: string, arg0: any): void;
export function return_call_service_result(wasm: any, ret: string, arg0: any): void;

View File

@ -91,6 +91,14 @@ cachedTextDecoder.decode();
export function getStringFromWasm0(wasm, ptr, len) {
return cachedTextDecoder.decode(getUint8Memory0(wasm).subarray(ptr, ptr + len));
}
function passArray8ToWasm0(wasm, arg, malloc) {
const ptr = malloc(arg.length * 1);
getUint8Memory0(wasm).set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
/**
* @param {any} wasm
* @param {string} init_user_id
@ -106,9 +114,9 @@ export function invoke(wasm, init_user_id, aqua, prev_data, data, log_level) {
var len0 = WASM_VECTOR_LEN;
var ptr1 = passStringToWasm0(wasm, aqua, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len1 = WASM_VECTOR_LEN;
var ptr2 = passStringToWasm0(wasm, prev_data, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var ptr2 = passArray8ToWasm0(wasm, prev_data, wasm.__wbindgen_malloc);
var len2 = WASM_VECTOR_LEN;
var ptr3 = passStringToWasm0(wasm, data, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var ptr3 = passArray8ToWasm0(wasm, data, wasm.__wbindgen_malloc);
var len3 = WASM_VECTOR_LEN;
var ptr4 = passStringToWasm0(wasm, log_level, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len4 = WASM_VECTOR_LEN;

View File

@ -66,7 +66,7 @@ export class FluenceClient {
log.info(`Particle expired. Now: ${now}, ttl: ${particle.ttl}, ts: ${particle.timestamp}`);
} else {
// if there is no subscription yet, previous data is empty
let prevData = [];
let prevData: Uint8Array = Buffer.from([]);
let prevParticle = this.subscriptions.get(particle.id);
if (prevParticle) {
prevData = prevParticle.data;
@ -79,8 +79,8 @@ export class FluenceClient {
let stepperOutcomeStr = this.interpreter(
particle.init_peer_id,
particle.script,
JSON.stringify(prevData),
JSON.stringify(particle.data),
prevData,
particle.data,
);
let stepperOutcome: StepperOutcome = JSON.parse(stepperOutcomeStr);
@ -131,7 +131,7 @@ export class FluenceClient {
let _this = this;
return async (particle: Particle) => {
let data = particle.data;
let data: any = particle.data;
let error: any = data['protocol!error'];
if (error !== undefined) {
log.error('error in external particle: ');

View File

@ -23,7 +23,7 @@ import pipe from 'it-pipe';
import Multiaddr from 'multiaddr';
import PeerId from 'peer-id';
import * as log from 'loglevel';
import { build, parseParticle, Particle, stringifyParticle } from './particle';
import { build, parseParticle, Particle, toAction } from './particle';
export const PROTOCOL_NAME = '/fluence/faas/1.0.0';
@ -121,8 +121,9 @@ export class FluenceConnection {
async sendParticle(particle: Particle): Promise<void> {
this.checkConnectedOrThrow();
let particleStr = stringifyParticle(particle);
log.debug('send particle: \n' + JSON.stringify(particle, undefined, 2));
let action = toAction(particle)
let particleStr = JSON.stringify(action);
log.debug('send particle: \n' + JSON.stringify(action, undefined, 2));
// create outgoing substream
const conn = (await this.node.dialProtocol(this.address, PROTOCOL_NAME)) as {

View File

@ -15,6 +15,7 @@
*/
import { v4 as uuidv4 } from 'uuid';
import { fromByteArray } from 'base64-js';
import PeerId from 'peer-id';
import { encode } from 'bs58';
import { addData } from './dataStorage';
@ -29,7 +30,21 @@ export interface Particle {
script: string;
// sign upper fields
signature: string;
data: any;
data: Uint8Array;
}
/**
* Represents particle action to send to a node
*/
interface ParticleAction {
action: 'Particle'
id: string;
init_peer_id: string;
timestamp: number;
ttl: number;
script: string;
signature: number[];
data: string;
}
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
ttl: ttl,
script: script,
signature: '',
data: [],
data: Buffer.from([]),
};
particle.signature = await signParticle(peerId, particle);
@ -72,16 +87,20 @@ export async function build(peerId: PeerId, script: string, data: Map<string, an
}
/**
* Copies a particle and stringify it.
* Creates an action to send to a node.
*/
export function stringifyParticle(call: Particle): string {
let obj: any = { ...call };
obj.action = 'Particle';
// delete it after signatures will be implemented on nodes
obj.signature = [];
return JSON.stringify(obj);
export function toAction(particle: Particle): ParticleAction {
return {
action: "Particle",
id: particle.id,
init_peer_id: particle.init_peer_id,
timestamp: particle.timestamp,
ttl: particle.ttl,
script: particle.script,
// TODO: copy signature from a particle after signatures will be implemented on nodes
signature: [],
data: fromByteArray(particle.data)
};
}
export function parseParticle(str: string): Particle {

View File

@ -26,7 +26,7 @@ import Instance = WebAssembly.Instance;
import Exports = WebAssembly.Exports;
import ExportValue = WebAssembly.ExportValue;
export type InterpreterInvoke = (init_user_id: string, script: string, prev_data: string, data: string) => string;
export type InterpreterInvoke = (init_user_id: string, script: string, prev_data: Uint8Array, data: Uint8Array) => string;
type ImportObject = {
'./aquamarine_client_bg.js': {
// 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
});
let instance = await interpreterInstance(cfg);
return (init_user_id: string, script: string, prev_data: string, data: string) => {
return (init_user_id: string, script: string, prev_data: Uint8Array, data: Uint8Array) => {
let logLevel = log.getLevel();
let logLevelStr = 'info';
if (logLevel === 0) {

View File

@ -16,6 +16,6 @@
export interface StepperOutcome {
ret_code: number;
data: number[];
data: Uint8Array;
next_peer_pks: string[];
}