2023-10-17 22:14:08 +07:00
|
|
|
/**
|
2023-08-25 00:15:49 +07:00
|
|
|
* 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.
|
|
|
|
*/
|
2023-10-17 22:14:08 +07:00
|
|
|
|
2023-10-25 19:02:42 +07:00
|
|
|
import { fetchResource } from "@fluencelabs/js-client-isomorphic/fetcher";
|
|
|
|
import { getWorker } from "@fluencelabs/js-client-isomorphic/worker-resolver";
|
2023-10-17 22:14:08 +07:00
|
|
|
|
|
|
|
import { ClientPeer, makeClientPeerConfig } from "./clientPeer/ClientPeer.js";
|
2023-10-18 08:38:49 +07:00
|
|
|
import {
|
|
|
|
ClientConfig,
|
|
|
|
ConnectionState,
|
|
|
|
RelayOptions,
|
|
|
|
} from "./clientPeer/types.js";
|
2023-10-17 22:14:08 +07:00
|
|
|
import { callAquaFunction } from "./compilerSupport/callFunction.js";
|
|
|
|
import { registerService } from "./compilerSupport/registerService.js";
|
|
|
|
import { MarineBackgroundRunner } from "./marine/worker/index.js";
|
2023-10-17 23:52:30 +07:00
|
|
|
|
2023-10-18 08:38:49 +07:00
|
|
|
const DEFAULT_CDN_URL = "https://unpkg.com";
|
|
|
|
|
|
|
|
const createClient = async (
|
|
|
|
relay: RelayOptions,
|
2023-10-25 19:02:42 +07:00
|
|
|
config: ClientConfig = {},
|
2023-10-18 08:38:49 +07:00
|
|
|
): Promise<ClientPeer> => {
|
|
|
|
const CDNUrl = config.CDNUrl ?? DEFAULT_CDN_URL;
|
2023-10-17 22:14:08 +07:00
|
|
|
|
2023-10-18 08:38:49 +07:00
|
|
|
const fetchMarineJsWasm = async () => {
|
|
|
|
const resource = await fetchResource(
|
2023-11-06 17:42:24 +07:00
|
|
|
"@fluencelabs/marine-js",
|
2023-10-18 08:38:49 +07:00
|
|
|
"/dist/marine-js.wasm",
|
|
|
|
CDNUrl,
|
|
|
|
);
|
2023-10-17 22:14:08 +07:00
|
|
|
|
2023-10-18 08:38:49 +07:00
|
|
|
return resource.arrayBuffer();
|
|
|
|
};
|
|
|
|
|
|
|
|
const fetchAvmWasm = async () => {
|
|
|
|
const resource = await fetchResource(
|
2023-11-06 17:42:24 +07:00
|
|
|
"@fluencelabs/avm",
|
2023-10-18 08:38:49 +07:00
|
|
|
"/dist/avm.wasm",
|
|
|
|
CDNUrl,
|
|
|
|
);
|
|
|
|
|
|
|
|
return resource.arrayBuffer();
|
|
|
|
};
|
2023-10-17 22:14:08 +07:00
|
|
|
|
|
|
|
const marineJsWasm = await fetchMarineJsWasm();
|
|
|
|
const avmWasm = await fetchAvmWasm();
|
|
|
|
|
|
|
|
const marine = new MarineBackgroundRunner(
|
|
|
|
{
|
|
|
|
async getValue() {
|
2023-11-06 17:42:24 +07:00
|
|
|
return getWorker("@fluencelabs/marine-worker", CDNUrl);
|
2023-10-17 22:14:08 +07:00
|
|
|
},
|
|
|
|
start() {
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
},
|
|
|
|
stop() {
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
getValue() {
|
|
|
|
return marineJsWasm;
|
|
|
|
},
|
|
|
|
start(): Promise<void> {
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
},
|
|
|
|
stop(): Promise<void> {
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
getValue() {
|
|
|
|
return avmWasm;
|
|
|
|
},
|
|
|
|
start(): Promise<void> {
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
},
|
|
|
|
stop(): Promise<void> {
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const { keyPair, peerConfig, relayConfig } = await makeClientPeerConfig(
|
|
|
|
relay,
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
|
|
|
|
const client = new ClientPeer(peerConfig, relayConfig, keyPair, marine);
|
|
|
|
|
2023-10-25 19:02:42 +07:00
|
|
|
// TODO: Support node specific utils
|
|
|
|
// if (isNode) {
|
|
|
|
// doRegisterNodeUtils(client);
|
|
|
|
// }
|
2023-10-17 22:14:08 +07:00
|
|
|
|
|
|
|
await client.connect();
|
|
|
|
return client;
|
2023-08-25 00:15:49 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Public interface to Fluence Network
|
|
|
|
*/
|
2023-10-17 22:14:08 +07:00
|
|
|
interface FluencePublicApi {
|
|
|
|
defaultClient: ClientPeer | undefined;
|
2023-10-25 19:02:42 +07:00
|
|
|
connect: (relay: RelayOptions, config?: ClientConfig) => Promise<void>;
|
2023-10-17 22:14:08 +07:00
|
|
|
disconnect: () => Promise<void>;
|
|
|
|
onConnectionStateChange: (
|
|
|
|
handler: (state: ConnectionState) => void,
|
|
|
|
) => ConnectionState;
|
|
|
|
getClient: () => ClientPeer;
|
|
|
|
}
|
2023-08-25 00:15:49 +07:00
|
|
|
|
2023-10-17 22:14:08 +07:00
|
|
|
export const Fluence: FluencePublicApi = {
|
|
|
|
defaultClient: undefined,
|
|
|
|
/**
|
|
|
|
* Connect to the Fluence network
|
|
|
|
* @param relay - relay node to connect to
|
|
|
|
* @param config - client configuration
|
|
|
|
*/
|
|
|
|
connect: async function (relay, config) {
|
|
|
|
this.defaultClient = await createClient(relay, config);
|
|
|
|
},
|
2023-08-25 00:15:49 +07:00
|
|
|
|
2023-10-17 22:14:08 +07:00
|
|
|
/**
|
|
|
|
* Disconnect from the Fluence network
|
|
|
|
*/
|
|
|
|
disconnect: async function (): Promise<void> {
|
|
|
|
await this.defaultClient?.disconnect();
|
|
|
|
this.defaultClient = undefined;
|
|
|
|
},
|
2023-08-25 00:15:49 +07:00
|
|
|
|
2023-10-17 22:14:08 +07:00
|
|
|
/**
|
|
|
|
* Handle connection state changes. Immediately returns the current connection state
|
|
|
|
*/
|
|
|
|
onConnectionStateChange(handler) {
|
|
|
|
return (
|
|
|
|
this.defaultClient?.onConnectionStateChange(handler) ?? "disconnected"
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Low level API. Get the underlying client instance which holds the connection to the network
|
|
|
|
* @returns IFluenceClient instance
|
|
|
|
*/
|
|
|
|
getClient: function () {
|
|
|
|
if (this.defaultClient == null) {
|
|
|
|
throw new Error(
|
|
|
|
"Fluence client is not initialized. Call Fluence.connect() first",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.defaultClient;
|
|
|
|
},
|
2023-08-25 00:15:49 +07:00
|
|
|
};
|
|
|
|
|
2023-11-06 17:42:24 +07:00
|
|
|
export type {
|
|
|
|
ClientConfig,
|
|
|
|
IFluenceClient,
|
|
|
|
ConnectionState,
|
|
|
|
RelayOptions,
|
|
|
|
KeyPairOptions,
|
|
|
|
} from "./clientPeer/types.js";
|
2023-08-25 00:15:49 +07:00
|
|
|
|
2023-10-17 22:14:08 +07:00
|
|
|
export { v5_callFunction, v5_registerService } from "./api.js";
|
|
|
|
|
|
|
|
// @ts-expect-error Writing to global object like this prohibited by ts
|
2023-08-25 00:15:49 +07:00
|
|
|
globalThis.new_fluence = Fluence;
|
|
|
|
|
2023-10-17 22:14:08 +07:00
|
|
|
// @ts-expect-error Writing to global object like this prohibited by ts
|
2023-08-25 00:15:49 +07:00
|
|
|
globalThis.fluence = {
|
2023-10-17 22:14:08 +07:00
|
|
|
clientFactory: createClient,
|
|
|
|
callAquaFunction,
|
|
|
|
registerService,
|
2023-08-25 00:15:49 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
export { createClient, callAquaFunction, registerService };
|
2023-10-25 19:02:42 +07:00
|
|
|
|
|
|
|
// Deprecated exports. Later they will be exposed only under js-client/keypair path
|
2023-10-17 22:14:08 +07:00
|
|
|
export {
|
|
|
|
KeyPair,
|
|
|
|
fromBase64Sk,
|
|
|
|
fromBase58Sk,
|
|
|
|
fromOpts,
|
|
|
|
} from "./keypair/index.js";
|
2023-10-25 19:02:42 +07:00
|
|
|
|
|
|
|
export * from "./network.js";
|