mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-20 19:36:31 +00:00
fix: JS-client bugs and tech debt [fixes DXJ-520] (#374)
Fix various bugs and a pair of TODO's
This commit is contained in:
@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import type { VersionedPackage } from "../types.js";
|
||||
import { FetchedPackages, getVersionedPackage } from "../types.js";
|
||||
|
||||
/**
|
||||
* @param pkg name of package with version
|
||||
@ -22,7 +22,7 @@ import type { VersionedPackage } from "../types.js";
|
||||
* @param root CDN domain in browser or file system root in node
|
||||
*/
|
||||
export async function fetchResource(
|
||||
pkg: VersionedPackage,
|
||||
pkg: FetchedPackages,
|
||||
assetPath: string,
|
||||
root: string,
|
||||
) {
|
||||
@ -30,7 +30,8 @@ export async function fetchResource(
|
||||
? assetPath.slice(1)
|
||||
: assetPath;
|
||||
|
||||
const url = new URL(`${pkg.name}@${pkg.version}/` + refinedAssetPath, root);
|
||||
const { name, version } = getVersionedPackage(pkg);
|
||||
const url = new URL(`${name}@${version}/` + refinedAssetPath, root);
|
||||
|
||||
return fetch(url).catch(() => {
|
||||
throw new Error(`Cannot fetch from ${url.toString()}`);
|
||||
|
@ -18,7 +18,7 @@ import { readFile } from "fs/promises";
|
||||
import { createRequire } from "module";
|
||||
import { sep, posix, join } from "path";
|
||||
|
||||
import type { VersionedPackage } from "../types.js";
|
||||
import { FetchedPackages, getVersionedPackage } from "../types.js";
|
||||
|
||||
/**
|
||||
* @param pkg name of package with version
|
||||
@ -26,24 +26,25 @@ import type { VersionedPackage } from "../types.js";
|
||||
* @param root CDN domain in browser or js-client itself in node
|
||||
*/
|
||||
export async function fetchResource(
|
||||
pkg: VersionedPackage,
|
||||
pkg: FetchedPackages,
|
||||
assetPath: string,
|
||||
root: string,
|
||||
) {
|
||||
const { name } = getVersionedPackage(pkg);
|
||||
// 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);
|
||||
const packagePathIndex = require.resolve(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 matches = new RegExp(`(.+${name})`).exec(posixPath);
|
||||
|
||||
const packagePath = matches?.[0];
|
||||
|
||||
if (packagePath == null) {
|
||||
throw new Error(`Cannot find dependency ${pkg.name} in path ${posixPath}`);
|
||||
throw new Error(`Cannot find dependency ${name} in path ${posixPath}`);
|
||||
}
|
||||
|
||||
const pathToResource = join(root, packagePath, assetPath);
|
||||
|
@ -16,8 +16,18 @@
|
||||
|
||||
import { Worker } from "@fluencelabs/threads/master";
|
||||
|
||||
export type VersionedPackage = { name: string; version: string };
|
||||
import versions from "./versions.js";
|
||||
|
||||
export type FetchedPackages = keyof typeof versions;
|
||||
type VersionedPackage = { name: string; version: string };
|
||||
export type GetWorker = (
|
||||
pkg: VersionedPackage,
|
||||
pkg: FetchedPackages,
|
||||
CDNUrl: string,
|
||||
) => Promise<Worker>;
|
||||
|
||||
export const getVersionedPackage = (pkg: FetchedPackages): VersionedPackage => {
|
||||
return {
|
||||
name: pkg,
|
||||
version: versions[pkg],
|
||||
};
|
||||
};
|
||||
|
@ -17,10 +17,10 @@
|
||||
import { BlobWorker } from "@fluencelabs/threads/master";
|
||||
|
||||
import { fetchResource } from "../fetchers/browser.js";
|
||||
import type { GetWorker, VersionedPackage } from "../types.js";
|
||||
import type { FetchedPackages, GetWorker } from "../types.js";
|
||||
|
||||
export const getWorker: GetWorker = async (
|
||||
pkg: VersionedPackage,
|
||||
pkg: FetchedPackages,
|
||||
CDNUrl: string,
|
||||
) => {
|
||||
const fetchWorkerCode = async () => {
|
||||
|
@ -20,14 +20,16 @@ import { fileURLToPath } from "url";
|
||||
|
||||
import { Worker } from "@fluencelabs/threads/master";
|
||||
|
||||
import type { GetWorker, VersionedPackage } from "../types.js";
|
||||
import type { FetchedPackages, GetWorker } from "../types.js";
|
||||
import { getVersionedPackage } from "../types.js";
|
||||
|
||||
export const getWorker: GetWorker = (pkg: VersionedPackage) => {
|
||||
export const getWorker: GetWorker = (pkg: FetchedPackages) => {
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
const pathToThisFile = dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
const pathToWorker = require.resolve(pkg.name);
|
||||
const { name } = getVersionedPackage(pkg);
|
||||
const pathToWorker = require.resolve(name);
|
||||
|
||||
const relativePathToWorker = relative(pathToThisFile, pathToWorker);
|
||||
|
||||
|
Reference in New Issue
Block a user