mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-19 11:01:21 +00:00
chore(js-client)!: Simplify/optimize js-client and update README [fixes DXJ-490] (#366)
* Update README * Improve build experience * Fix eslint * Fix eslint * Fix eslint * Fix tooling * Fix formatting * Fix formatting * Fix test resource name * Remove async * Fix comments * Add ts-check * Add new line * Fix arg * add todo's * Fix lint * Fix typo * Type module * Add deps to isomorphic package * Extract function type * Fix newline * Remove private * Use prepare hook instead of postinstall
This commit is contained in:
38
packages/core/js-client-isomorphic/src/fetchers/browser.ts
Normal file
38
packages/core/js-client-isomorphic/src/fetchers/browser.ts
Normal file
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* 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 type { VersionedPackage } from "../types.js";
|
||||
|
||||
/**
|
||||
* @param pkg name of package with version
|
||||
* @param assetPath path of required asset in given package
|
||||
* @param root CDN domain in browser or file system root in node
|
||||
*/
|
||||
export async function fetchResource(
|
||||
pkg: VersionedPackage,
|
||||
assetPath: string,
|
||||
root: string,
|
||||
) {
|
||||
const refinedAssetPath = assetPath.startsWith("/")
|
||||
? assetPath.slice(1)
|
||||
: assetPath;
|
||||
|
||||
const url = new URL(`${pkg.name}@${pkg.version}/` + refinedAssetPath, root);
|
||||
|
||||
return fetch(url).catch(() => {
|
||||
throw new Error(`Cannot fetch from ${url.toString()}`);
|
||||
});
|
||||
}
|
62
packages/core/js-client-isomorphic/src/fetchers/node.ts
Normal file
62
packages/core/js-client-isomorphic/src/fetchers/node.ts
Normal file
@ -0,0 +1,62 @@
|
||||
/**
|
||||
* 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 { readFile } from "fs/promises";
|
||||
import { createRequire } from "module";
|
||||
import { sep, posix, join } from "path";
|
||||
|
||||
import type { VersionedPackage } from "../types.js";
|
||||
|
||||
/**
|
||||
* @param pkg name of package with version
|
||||
* @param assetPath path of required asset in given package
|
||||
* @param root CDN domain in browser or js-client itself in node
|
||||
*/
|
||||
export async function fetchResource(
|
||||
pkg: VersionedPackage,
|
||||
assetPath: string,
|
||||
root: string,
|
||||
) {
|
||||
// TODO: `root` will be handled somehow in the future. For now, we use filesystem root where js-client is running;
|
||||
root = "/";
|
||||
const require = createRequire(import.meta.url);
|
||||
const packagePathIndex = require.resolve(pkg.name);
|
||||
|
||||
// Ensure that windows path is converted to posix path. So we can find a package
|
||||
const posixPath = packagePathIndex.split(sep).join(posix.sep);
|
||||
|
||||
const matches = new RegExp(`(.+${pkg.name})`).exec(posixPath);
|
||||
|
||||
const packagePath = matches?.[0];
|
||||
|
||||
if (packagePath == null) {
|
||||
throw new Error(`Cannot find dependency ${pkg.name} in path ${posixPath}`);
|
||||
}
|
||||
|
||||
const pathToResource = join(root, packagePath, assetPath);
|
||||
|
||||
const file = await readFile(pathToResource);
|
||||
|
||||
return new Response(file, {
|
||||
headers: {
|
||||
"Content-type": assetPath.endsWith(".wasm")
|
||||
? "application/wasm"
|
||||
: assetPath.endsWith(".js")
|
||||
? "application/javascript"
|
||||
: "application/text",
|
||||
},
|
||||
});
|
||||
}
|
23
packages/core/js-client-isomorphic/src/types.ts
Normal file
23
packages/core/js-client-isomorphic/src/types.ts
Normal file
@ -0,0 +1,23 @@
|
||||
/**
|
||||
* 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 { Worker } from "threads/master";
|
||||
|
||||
export type VersionedPackage = { name: string; version: string };
|
||||
export type GetWorker = (
|
||||
pkg: VersionedPackage,
|
||||
CDNUrl: string,
|
||||
) => Promise<Worker>;
|
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* 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 { BlobWorker } from "threads/master";
|
||||
|
||||
import { fetchResource } from "../fetchers/browser.js";
|
||||
import type { GetWorker, VersionedPackage } from "../types.js";
|
||||
|
||||
export const getWorker: GetWorker = async (
|
||||
pkg: VersionedPackage,
|
||||
CDNUrl: string,
|
||||
) => {
|
||||
const fetchWorkerCode = async () => {
|
||||
const resource = await fetchResource(
|
||||
pkg,
|
||||
"/dist/browser/marine-worker.umd.cjs",
|
||||
CDNUrl,
|
||||
);
|
||||
|
||||
return resource.text();
|
||||
};
|
||||
|
||||
const workerCode = await fetchWorkerCode();
|
||||
return BlobWorker.fromText(workerCode);
|
||||
};
|
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* 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 { createRequire } from "module";
|
||||
import { dirname, relative } from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
|
||||
import { Worker } from "threads/master";
|
||||
|
||||
import type { GetWorker, VersionedPackage } from "../types.js";
|
||||
|
||||
export const getWorker: GetWorker = (pkg: VersionedPackage) => {
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
const pathToThisFile = dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
const pathToWorker = require.resolve(pkg.name);
|
||||
|
||||
const relativePathToWorker = relative(pathToThisFile, pathToWorker);
|
||||
|
||||
return Promise.resolve(new Worker(relativePathToWorker));
|
||||
};
|
Reference in New Issue
Block a user