fix: adding some polyfills to nodejs-client and mix-fetch

During the merge of the nodejs-wasm-client there seems to be some
losses, this commit makes nodejs-client and the nodejs mix-fetch work
with the internal tools. Still looking into Tommys qa feedback.
This commit is contained in:
Sebastian Martinez
2023-11-01 15:57:23 +01:00
parent 05a39aa5f5
commit 4262e2e2f6
6 changed files with 79 additions and 12 deletions
@@ -0,0 +1,54 @@
const { createMixFetch, disconnectMixFetch } = require('../dist/cjs/index.js');
/**
* The main entry point
*/
(async () => {
console.log('Tester is starting up...');
const addr =
'D274yd1h3L3pNJzdxE5VgJ7izAsAVMsDrQtFSkKUegfk.8J67cGbcwvrJKF3Kb16HVWWc9AnrFnEibNCm9zCkuVFu@Emswx6KXyjRfq1c2k4d4uD2e6nBSbH1biorCZUei8UNS';
console.log('About to set up mixFetch...');
const { mixFetch } = await createMixFetch({
preferredNetworkRequester: addr,
clientId: 'node-client1',
clientOverride: {
coverTraffic: { disableLoopCoverTrafficStream: true },
traffic: { disableMainPoissonPacketDistribution: true },
},
mixFetchOverride: { requestTimeoutMs: 60000 },
responseBodyConfigMap: {},
extra: {},
});
globalThis.mixFetch = mixFetch;
if (!globalThis.mixFetch) {
console.error('Oh no! Could not create mixFetch');
} else {
console.log('Ready!');
}
let url = 'https://nymtech.net/.wellknown/network-requester/standard-allowed-list.txt';
console.log(`Using mixFetch to get ${url}...`);
const args = { mode: 'unsafe-ignore-cors' };
let resp = await mixFetch(url, args);
console.log({ resp });
const text = await resp.text();
console.log('disconnecting');
await disconnectMixFetch();
console.log('disconnected! all further usages should fail');
// get an image
url = 'https://nymtech.net/favicon.svg';
resp = await mixFetch(url, args);
console.log({ resp });
const buffer = await resp.arrayBuffer();
const type = resp.headers.get('Content-Type') || 'image/svg';
const blobUrl = URL.createObjectURL(new Blob([buffer], { type }));
console.log(JSON.stringify({ bufferBytes: buffer.byteLength, blobUrl }, null, 2));
console.log(blobUrl);
})();
@@ -11,7 +11,7 @@ export default {
dir: 'dist/cjs',
format: 'cjs',
},
onwarn,
external: ['util', 'fake-indexeddb'],
plugins: [
resolve({
browser: false,
@@ -19,6 +19,10 @@ export default {
extensions: ['.js', '.ts'],
}),
commonjs(),
modify({
find: 'const ret = new WebSocket(getStringFromWasm0(arg0, arg1));',
replace: 'const ws = require("ws"); const ret = new ws.WebSocket(getStringFromWasm0(arg0, arg1));',
}),
// TODO: `getObject(...).require` seems to generate a warning on Webpack but with Rollup we get a panic since it can't require.
// By hard coding the require here, we can workaround that.
// Reference: https://github.com/rust-random/getrandom/issues/224
@@ -33,11 +37,3 @@ export default {
}),
],
};
function onwarn(warning) {
// fake-indexeddb has a circular dependency that triggers a warning when rolled up
if (warning.code !== 'CIRCULAR_DEPENDENCY') {
// eslint-disable-next-line no-console
console.error(`(!) ${warning.message}`);
}
}
@@ -12,6 +12,7 @@ export default {
format: 'cjs',
},
plugins: [
resolve({ browser: false, extensions: ['.js', '.ts'] }),
webWorkerLoader({ targetPlatform: 'node', inline: false }),
replace({
values: {
@@ -21,7 +22,6 @@ export default {
delimiters: ['', ''],
preventAssignment: true,
}),
resolve({ browser: false, extensions: ['.js', '.ts'] }),
wasm({ targetEnv: 'node', maxFileSize: 0 }),
typescript({
compilerOptions: { outDir: 'dist/cjs', target: 'es5' },
@@ -11,6 +11,7 @@ export default {
dir: 'dist/cjs',
format: 'cjs',
},
external: ['util', 'fake-indexeddb'],
plugins: [
resolve({
browser: false,
@@ -18,6 +19,11 @@ export default {
extensions: ['.js', '.ts'],
}),
commonjs(),
// TODO: One of the wasm functions calls `new WebSocket` at one point, which we aren't able to polyfill correctly yet.
modify({
find: 'const ret = new WebSocket(getStringFromWasm0(arg0, arg1));',
replace: 'const ws = require("ws"); const ret = new ws.WebSocket(getStringFromWasm0(arg0, arg1));',
}),
// TODO: `getObject(...).require` seems to generate a warning on Webpack but with Rollup we get a panic since it can't require.
// By hard coding the require here, we can workaround that.
// Reference: https://github.com/rust-random/getrandom/issues/224
@@ -1,6 +1,6 @@
import * as crypto from 'node:crypto';
import * as fs from 'node:fs';
import ws from 'ws';
import WebSocket from 'ws';
import { TextDecoder, TextEncoder } from 'node:util';
import { Worker } from 'node:worker_threads';
import { indexedDB } from 'fake-indexeddb';
@@ -10,7 +10,7 @@ import { performance } from 'node:perf_hooks';
(globalThis as any).TextDecoder = TextDecoder;
(globalThis as any).fs = fs;
(globalThis as any).crypto = crypto;
(globalThis as any).ws = ws;
(globalThis as any).WebSocket = WebSocket;
(globalThis as any).Worker = Worker;
globalThis.process = process;
+11
View File
@@ -13227,6 +13227,17 @@ jest@^29.5.0:
import-local "^3.0.2"
jest-cli "^29.7.0"
joi@^17.11.0:
version "17.11.0"
resolved "https://registry.yarnpkg.com/joi/-/joi-17.11.0.tgz#aa9da753578ec7720e6f0ca2c7046996ed04fc1a"
integrity sha512-NgB+lZLNoqISVy1rZocE9PZI36bL/77ie924Ri43yEvi9GUUMPeyVIr8KdFTMUlby1p0PBYMk9spIxEUQYqrJQ==
dependencies:
"@hapi/hoek" "^9.0.0"
"@hapi/topo" "^5.0.0"
"@sideway/address" "^4.1.3"
"@sideway/formula" "^3.0.1"
"@sideway/pinpoint" "^2.0.0"
js-sha3@^0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/js-sha3/-/js-sha3-0.8.0.tgz#b9b7a5da73afad7dedd0f8c463954cbde6818840"