mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-16 17:41:21 +00:00
* introduce eslint * Fix all eslint errors * Eslint fix and some touches * Fix tests * Fix misc errors * change semver * change semver #2 * Fix path * Fix path #2 * freeze lock file in CI * fix package install * Fix formatting of surrounding files * Add empty prettier config * Fix formatting * Fix build errors * Remove unused deps * remove changelog from formatting * deps cleanup * make resource importers async * Refactor * Fix error message * remove comment * more refactoring * Update packages/core/js-client/src/compilerSupport/registerService.ts Co-authored-by: shamsartem <shamsartem@gmail.com> * refactoring * refactoring fix * optimize import * Update packages/@tests/smoke/node/src/index.ts Co-authored-by: shamsartem <shamsartem@gmail.com> * Revert package * Fix pnpm lock * Lint-fix * Fix CI * Update tests * Fix build * Fix import * Use forked threads dep * Use fixed version * Update threads * Fix lint * Fix test * Fix test * Add polyfill for assert * Add subpath import * Fix tests * Fix deps --------- Co-authored-by: shamsartem <shamsartem@gmail.com>
112 lines
3.0 KiB
TypeScript
112 lines
3.0 KiB
TypeScript
/**
|
|
* Copyright 2023 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.
|
|
*/
|
|
|
|
import { Buffer } from "buffer";
|
|
|
|
import { CallParams } from "@fluencelabs/interfaces";
|
|
import { v4 as uuidv4 } from "uuid";
|
|
|
|
import { FluencePeer } from "../jsPeer/FluencePeer.js";
|
|
import { getErrorMessage } from "../util/utils.js";
|
|
|
|
import { SrvDef } from "./_aqua/single-module-srv.js";
|
|
import {
|
|
allowOnlyParticleOriginatedAt,
|
|
SecurityGuard,
|
|
} from "./securityGuard.js";
|
|
|
|
export const defaultGuard = (peer: FluencePeer) => {
|
|
return allowOnlyParticleOriginatedAt(peer.keyPair.getPeerId());
|
|
};
|
|
|
|
export class Srv implements SrvDef {
|
|
private services: Set<string> = new Set();
|
|
|
|
constructor(private peer: FluencePeer) {
|
|
this.securityGuard_create = defaultGuard(this.peer);
|
|
this.securityGuard_remove = defaultGuard(this.peer);
|
|
}
|
|
|
|
securityGuard_create: SecurityGuard<"wasm_b64_content">;
|
|
|
|
async create(
|
|
wasm_b64_content: string,
|
|
callParams: CallParams<"wasm_b64_content">,
|
|
) {
|
|
if (!this.securityGuard_create(callParams)) {
|
|
return {
|
|
success: false,
|
|
error: "Security guard validation failed",
|
|
service_id: null,
|
|
};
|
|
}
|
|
|
|
try {
|
|
const newServiceId = uuidv4();
|
|
const buffer = Buffer.from(wasm_b64_content, "base64");
|
|
// TODO:: figure out why SharedArrayBuffer is not working here
|
|
// const sab = new SharedArrayBuffer(buffer.length);
|
|
// const tmp = new Uint8Array(sab);
|
|
// tmp.set(buffer, 0);
|
|
await this.peer.registerMarineService(buffer, newServiceId);
|
|
this.services.add(newServiceId);
|
|
|
|
return {
|
|
success: true,
|
|
service_id: newServiceId,
|
|
error: null,
|
|
};
|
|
} catch (err: unknown) {
|
|
return {
|
|
success: true,
|
|
service_id: null,
|
|
error: getErrorMessage(err),
|
|
};
|
|
}
|
|
}
|
|
|
|
securityGuard_remove: SecurityGuard<"service_id">;
|
|
|
|
async remove(service_id: string, callParams: CallParams<"service_id">) {
|
|
if (!this.securityGuard_remove(callParams)) {
|
|
return {
|
|
success: false,
|
|
error: "Security guard validation failed",
|
|
service_id: null,
|
|
};
|
|
}
|
|
|
|
if (!this.services.has(service_id)) {
|
|
return {
|
|
success: false,
|
|
error: `Service with id ${service_id} not found`,
|
|
};
|
|
}
|
|
|
|
await this.peer.removeMarineService(service_id);
|
|
this.services.delete(service_id);
|
|
|
|
return {
|
|
success: true,
|
|
error: null,
|
|
};
|
|
}
|
|
|
|
list() {
|
|
return Array.from(this.services.values());
|
|
}
|
|
}
|