mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-28 15:21:33 +00:00
Data as base64 (#2)
This commit is contained in:
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "fluence",
|
"name": "fluence",
|
||||||
"version": "0.7.103",
|
"version": "0.7.106",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "fluence",
|
"name": "fluence",
|
||||||
"version": "0.7.106",
|
"version": "0.7.107",
|
||||||
"description": "the browser js-libp2p client for the Fluence network",
|
"description": "the browser js-libp2p client for the Fluence network",
|
||||||
"main": "./dist/fluence.js",
|
"main": "./dist/fluence.js",
|
||||||
"typings": "./dist/fluence.d.ts",
|
"typings": "./dist/fluence.d.ts",
|
||||||
|
2
src/aqua/index.d.ts
vendored
2
src/aqua/index.d.ts
vendored
@ -9,7 +9,7 @@
|
|||||||
* @param {string} log_level
|
* @param {string} log_level
|
||||||
* @returns {string}
|
* @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 ast(wasm: any, script: string): string;
|
||||||
export function return_current_peer_id(wasm: any, peerId: string, arg0: any): void;
|
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;
|
export function return_call_service_result(wasm: any, ret: string, arg0: any): void;
|
||||||
|
@ -91,6 +91,14 @@ cachedTextDecoder.decode();
|
|||||||
export function getStringFromWasm0(wasm, ptr, len) {
|
export function getStringFromWasm0(wasm, ptr, len) {
|
||||||
return cachedTextDecoder.decode(getUint8Memory0(wasm).subarray(ptr, 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 {any} wasm
|
||||||
* @param {string} init_user_id
|
* @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 len0 = WASM_VECTOR_LEN;
|
||||||
var ptr1 = passStringToWasm0(wasm, aqua, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
var ptr1 = passStringToWasm0(wasm, aqua, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
||||||
var len1 = WASM_VECTOR_LEN;
|
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 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 len3 = WASM_VECTOR_LEN;
|
||||||
var ptr4 = passStringToWasm0(wasm, log_level, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
var ptr4 = passStringToWasm0(wasm, log_level, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
||||||
var len4 = WASM_VECTOR_LEN;
|
var len4 = WASM_VECTOR_LEN;
|
||||||
|
@ -66,7 +66,7 @@ export class FluenceClient {
|
|||||||
log.info(`Particle expired. Now: ${now}, ttl: ${particle.ttl}, ts: ${particle.timestamp}`);
|
log.info(`Particle expired. Now: ${now}, ttl: ${particle.ttl}, ts: ${particle.timestamp}`);
|
||||||
} else {
|
} else {
|
||||||
// if there is no subscription yet, previous data is empty
|
// if there is no subscription yet, previous data is empty
|
||||||
let prevData = [];
|
let prevData: Uint8Array = Buffer.from([]);
|
||||||
let prevParticle = this.subscriptions.get(particle.id);
|
let prevParticle = this.subscriptions.get(particle.id);
|
||||||
if (prevParticle) {
|
if (prevParticle) {
|
||||||
prevData = prevParticle.data;
|
prevData = prevParticle.data;
|
||||||
@ -79,8 +79,8 @@ export class FluenceClient {
|
|||||||
let stepperOutcomeStr = this.interpreter(
|
let stepperOutcomeStr = this.interpreter(
|
||||||
particle.init_peer_id,
|
particle.init_peer_id,
|
||||||
particle.script,
|
particle.script,
|
||||||
JSON.stringify(prevData),
|
prevData,
|
||||||
JSON.stringify(particle.data),
|
particle.data,
|
||||||
);
|
);
|
||||||
let stepperOutcome: StepperOutcome = JSON.parse(stepperOutcomeStr);
|
let stepperOutcome: StepperOutcome = JSON.parse(stepperOutcomeStr);
|
||||||
|
|
||||||
@ -131,7 +131,7 @@ export class FluenceClient {
|
|||||||
let _this = this;
|
let _this = this;
|
||||||
|
|
||||||
return async (particle: Particle) => {
|
return async (particle: Particle) => {
|
||||||
let data = particle.data;
|
let data: any = particle.data;
|
||||||
let error: any = data['protocol!error'];
|
let error: any = data['protocol!error'];
|
||||||
if (error !== undefined) {
|
if (error !== undefined) {
|
||||||
log.error('error in external particle: ');
|
log.error('error in external particle: ');
|
||||||
|
@ -23,7 +23,7 @@ import pipe from 'it-pipe';
|
|||||||
import Multiaddr from 'multiaddr';
|
import Multiaddr from 'multiaddr';
|
||||||
import PeerId from 'peer-id';
|
import PeerId from 'peer-id';
|
||||||
import * as log from 'loglevel';
|
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';
|
export const PROTOCOL_NAME = '/fluence/faas/1.0.0';
|
||||||
|
|
||||||
@ -121,8 +121,9 @@ export class FluenceConnection {
|
|||||||
async sendParticle(particle: Particle): Promise<void> {
|
async sendParticle(particle: Particle): Promise<void> {
|
||||||
this.checkConnectedOrThrow();
|
this.checkConnectedOrThrow();
|
||||||
|
|
||||||
let particleStr = stringifyParticle(particle);
|
let action = toAction(particle)
|
||||||
log.debug('send particle: \n' + JSON.stringify(particle, undefined, 2));
|
let particleStr = JSON.stringify(action);
|
||||||
|
log.debug('send particle: \n' + JSON.stringify(action, undefined, 2));
|
||||||
|
|
||||||
// create outgoing substream
|
// create outgoing substream
|
||||||
const conn = (await this.node.dialProtocol(this.address, PROTOCOL_NAME)) as {
|
const conn = (await this.node.dialProtocol(this.address, PROTOCOL_NAME)) as {
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
import { fromByteArray } from 'base64-js';
|
||||||
import PeerId from 'peer-id';
|
import PeerId from 'peer-id';
|
||||||
import { encode } from 'bs58';
|
import { encode } from 'bs58';
|
||||||
import { addData } from './dataStorage';
|
import { addData } from './dataStorage';
|
||||||
@ -29,7 +30,21 @@ export interface Particle {
|
|||||||
script: string;
|
script: string;
|
||||||
// sign upper fields
|
// sign upper fields
|
||||||
signature: string;
|
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 {
|
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,
|
ttl: ttl,
|
||||||
script: script,
|
script: script,
|
||||||
signature: '',
|
signature: '',
|
||||||
data: [],
|
data: Buffer.from([]),
|
||||||
};
|
};
|
||||||
|
|
||||||
particle.signature = await signParticle(peerId, particle);
|
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 {
|
export function toAction(particle: Particle): ParticleAction {
|
||||||
let obj: any = { ...call };
|
return {
|
||||||
obj.action = 'Particle';
|
action: "Particle",
|
||||||
|
id: particle.id,
|
||||||
// delete it after signatures will be implemented on nodes
|
init_peer_id: particle.init_peer_id,
|
||||||
obj.signature = [];
|
timestamp: particle.timestamp,
|
||||||
|
ttl: particle.ttl,
|
||||||
return JSON.stringify(obj);
|
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 {
|
export function parseParticle(str: string): Particle {
|
||||||
|
@ -26,7 +26,7 @@ import Instance = WebAssembly.Instance;
|
|||||||
import Exports = WebAssembly.Exports;
|
import Exports = WebAssembly.Exports;
|
||||||
import ExportValue = WebAssembly.ExportValue;
|
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 = {
|
type ImportObject = {
|
||||||
'./aquamarine_client_bg.js': {
|
'./aquamarine_client_bg.js': {
|
||||||
// fn call_service_impl(service_id: String, fn_name: String, args: String, security_tetraplets: String) -> String;
|
// 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);
|
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 logLevel = log.getLevel();
|
||||||
let logLevelStr = 'info';
|
let logLevelStr = 'info';
|
||||||
if (logLevel === 0) {
|
if (logLevel === 0) {
|
||||||
|
@ -16,6 +16,6 @@
|
|||||||
|
|
||||||
export interface StepperOutcome {
|
export interface StepperOutcome {
|
||||||
ret_code: number;
|
ret_code: number;
|
||||||
data: number[];
|
data: Uint8Array;
|
||||||
next_peer_pks: string[];
|
next_peer_pks: string[];
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user