chore: fix build [fixes DXJ-482] (#352)

* Remove additional node targeted build

* Fix test errors

* Typo fix

* Remove headless

* Prevent JSON error

* Cache json parsing

* Remove resource test as it's not working in new nox

* Fix test output

* enable smoke tests

* add puppeteer to deps

* Remove headless option
This commit is contained in:
Akim
2023-09-29 16:23:06 +07:00
committed by GitHub
parent 63e4ce3f84
commit 15a2c91917
22 changed files with 414 additions and 706 deletions

View File

@ -14,6 +14,27 @@
* limitations under the License.
*/
export async function fetchResource(assetPath: string, version: string) {
return fetch(new globalThis.URL(`@fluencelabs/js-client@${version}/dist` + assetPath, `https://unpkg.com/`));
interface PackageJsonContent {
dependencies: Record<string, string | undefined>;
devDependencies: Record<string, string | undefined>;
}
// This will be substituted in build phase
const packageJsonContentString = `__PACKAGE_JSON_CONTENT__`;
let parsedPackageJsonContent: PackageJsonContent;
const PRIMARY_CDN = "https://unpkg.com/";
export async function fetchResource(pkg: string, assetPath: string) {
const packageJsonContent = parsedPackageJsonContent || (parsedPackageJsonContent = JSON.parse(packageJsonContentString));
const version = packageJsonContent.dependencies[pkg] || packageJsonContent.devDependencies[pkg];
if (version === undefined) {
const availableDeps = [...Object.keys(packageJsonContent.dependencies), ...Object.keys(packageJsonContent.devDependencies)];
throw new Error(`Cannot find version of ${pkg} in package.json. Available versions: ${availableDeps.join(',')}`);
}
const refinedAssetPath = assetPath.startsWith('/') ? assetPath.slice(1) : assetPath;
return fetch(new globalThis.URL(`${pkg}@${version}/` + refinedAssetPath, PRIMARY_CDN));
}

View File

@ -20,11 +20,11 @@ import process from 'process';
const isNode = typeof process !== 'undefined' && process?.release?.name === 'node';
export async function fetchResource(assetPath: string, version: string) {
export async function fetchResource(pkg: string, path: string) {
switch (true) {
case isNode:
return fetchResourceNode(assetPath, version);
return fetchResourceNode(pkg, path);
default:
return fetchResourceBrowser(assetPath, version);
return fetchResourceBrowser(pkg, path);
}
}

View File

@ -15,22 +15,37 @@
*/
import fs from 'fs';
import url from 'url';
import path from 'path';
import module from 'module';
export async function fetchResource(assetPath: string, version: string) {
export async function fetchResource(pkg: string, assetPath: string) {
const require = module.createRequire(import.meta.url);
const packagePathIndex = require.resolve(pkg);
// Ensure that windows path is converted to posix path. So we can find a package
const posixPath = packagePathIndex.split(path.sep).join(path.posix.sep);
const matches = new RegExp(`(.+${pkg})`).exec(posixPath);
const packagePath = matches?.[0];
if (!packagePath) {
throw new Error(`Cannot find dependency ${pkg} in path ${posixPath}`);
}
const pathToResource = path.join(packagePath, assetPath);
const file = await new Promise<ArrayBuffer>((resolve, reject) => {
// Cannot use 'fs/promises' with current vite config. This module is not polyfilled by default.
const root = path.dirname(url.fileURLToPath(import.meta.url));
const workerFilePath = path.join(root, '..', assetPath);
fs.readFile(workerFilePath, (err, data) => {
fs.readFile(pathToResource, (err, data) => {
if (err) {
reject(err);
return;
}
resolve(data);
});
});
});
return new Response(file, {
headers: {
'Content-type':

View File

@ -23,28 +23,32 @@ import { BlobWorker, Worker } from 'threads';
import { doRegisterNodeUtils } from './services/NodeUtils.js';
import { fetchResource } from './fetchers/index.js';
import process from 'process';
import avmWasmUrl from '../node_modules/@fluencelabs/avm/dist/avm.wasm?url';
import marineJsWasmUrl from '../node_modules/@fluencelabs/marine-js/dist/marine-js.wasm?url';
import workerCodeUrl from '../node_modules/@fluencelabs/marine-worker/dist/__ENV__/marine-worker.umd.cjs?url';
const JS_CLIENT_VERSION = '__JS_CLIENT_VERSION__';
import path from 'path';
import url from 'url';
import module from 'module';
const isNode = typeof process !== 'undefined' && process?.release?.name === 'node';
const fetchWorkerCode = () => fetchResource(workerCodeUrl, JS_CLIENT_VERSION).then(res => res.text());
const fetchMarineJsWasm = () => fetchResource(marineJsWasmUrl, JS_CLIENT_VERSION).then(res => res.arrayBuffer());
const fetchAvmWasm = () => fetchResource(avmWasmUrl, JS_CLIENT_VERSION).then(res => res.arrayBuffer());
const fetchWorkerCode = () => fetchResource('@fluencelabs/marine-worker', '/dist/browser/marine-worker.umd.cjs').then(res => res.text());
const fetchMarineJsWasm = () => fetchResource('@fluencelabs/marine-js', '/dist/marine-js.wasm').then(res => res.arrayBuffer());
const fetchAvmWasm = () => fetchResource('@fluencelabs/avm', '/dist/avm.wasm').then(res => res.arrayBuffer());
const createClient = async (relay: RelayOptions, config: ClientConfig): Promise<IFluenceClient> => {
const workerCode = await fetchWorkerCode();
const marineJsWasm = await fetchMarineJsWasm();
const avmWasm = await fetchAvmWasm();
const marine = new MarineBackgroundRunner({
getValue() {
return BlobWorker.fromText(workerCode)
async getValue() {
if (isNode) {
const require = module.createRequire(import.meta.url);
const pathToThisFile = path.dirname(url.fileURLToPath(import.meta.url));
const pathToWorker = require.resolve('@fluencelabs/marine-worker');
const relativePathToWorker = path.relative(pathToThisFile, pathToWorker);
return new Worker(relativePathToWorker);
} else {
const workerCode = await fetchWorkerCode();
return BlobWorker.fromText(workerCode)
}
},
start() {
return Promise.resolve(undefined);

View File

@ -21,6 +21,6 @@ import { LazyLoader } from '../interfaces.js';
export class WorkerLoader extends LazyLoader<WorkerImplementation> {
constructor() {
super(() => new Worker('../../../node_modules/@fluencelabs/marine-worker/dist/node/marine-worker.umd.cjs'));
super(() => new Worker('../../../node_modules/@fluencelabs/marine-worker/dist/index.js'));
}
}

View File

@ -57,7 +57,7 @@ export class MarineBackgroundRunner implements IMarineHost {
await this.avmWasmLoader.start();
await this.workerLoader.start();
const worker = this.workerLoader.getValue();
const worker = await this.workerLoader.getValue();
const workerThread = await spawn<MarineBackgroundInterface>(worker);
const logfn: LogFunction = (message) => {