feat(js-client)!: Segregation of responsibility between js-client packages [fixes DXJ-525] (#378)

Schema validation in js-client
This commit is contained in:
Akim
2023-11-19 09:04:10 +07:00
committed by GitHub
parent 638da47bc2
commit f4a550dd22
80 changed files with 2998 additions and 11303 deletions

View File

@ -14,18 +14,14 @@
* limitations under the License.
*/
import { FetchedPackages, getVersionedPackage } from "../types.js";
import { FetchResourceFn, getVersionedPackage } 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: FetchedPackages,
assetPath: string,
root: string,
) {
export const fetchResource: FetchResourceFn = async (pkg, assetPath, root) => {
const refinedAssetPath = assetPath.startsWith("/")
? assetPath.slice(1)
: assetPath;
@ -36,4 +32,4 @@ export async function fetchResource(
return fetch(url).catch(() => {
throw new Error(`Cannot fetch from ${url.toString()}`);
});
}
};

View File

@ -18,21 +18,14 @@ import { readFile } from "fs/promises";
import { createRequire } from "module";
import { sep, posix, join } from "path";
import { FetchedPackages, getVersionedPackage } from "../types.js";
import { FetchResourceFn, getVersionedPackage } 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: FetchedPackages,
assetPath: string,
root: string,
) {
export const fetchResource: FetchResourceFn = async (pkg, assetPath) => {
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(name);
@ -47,7 +40,7 @@ export async function fetchResource(
throw new Error(`Cannot find dependency ${name} in path ${posixPath}`);
}
const pathToResource = join(root, packagePath, assetPath);
const pathToResource = join(packagePath, assetPath);
const file = await readFile(pathToResource);
@ -60,4 +53,4 @@ export async function fetchResource(
: "application/text",
},
});
}
};

View File

@ -20,7 +20,7 @@ import versions from "./versions.js";
export type FetchedPackages = keyof typeof versions;
type VersionedPackage = { name: string; version: string };
export type GetWorker = (
export type GetWorkerFn = (
pkg: FetchedPackages,
CDNUrl: string,
) => Promise<Worker>;
@ -31,3 +31,9 @@ export const getVersionedPackage = (pkg: FetchedPackages): VersionedPackage => {
version: versions[pkg],
};
};
export type FetchResourceFn = (
pkg: FetchedPackages,
assetPath: string,
root: string,
) => Promise<Response>;

View File

@ -17,9 +17,9 @@
import { BlobWorker } from "@fluencelabs/threads/master";
import { fetchResource } from "../fetchers/browser.js";
import type { FetchedPackages, GetWorker } from "../types.js";
import type { FetchedPackages, GetWorkerFn } from "../types.js";
export const getWorker: GetWorker = async (
export const getWorker: GetWorkerFn = async (
pkg: FetchedPackages,
CDNUrl: string,
) => {

View File

@ -20,10 +20,10 @@ import { fileURLToPath } from "url";
import { Worker } from "@fluencelabs/threads/master";
import type { FetchedPackages, GetWorker } from "../types.js";
import type { FetchedPackages, GetWorkerFn } from "../types.js";
import { getVersionedPackage } from "../types.js";
export const getWorker: GetWorker = (pkg: FetchedPackages) => {
export const getWorker: GetWorkerFn = (pkg: FetchedPackages) => {
const require = createRequire(import.meta.url);
const pathToThisFile = dirname(fileURLToPath(import.meta.url));