Files
nym/wasm/mix-fetch/internal-dev-node/src/polyfill.mjs
T
Jędrzej Stuczyński e7929d6f6b Feature/wasm client nodejs (#3769)
* wip

* post-cherry pick fixes

* wip

* wip

* using sqlite-based indexeddb shim

* running nymClient in worker thread

* improved received handling

* building node mix-fetch

* fixed mix fetch request constructor if args[1] == undefined

* fixed build target

* nodejs origin bypass

* mix fetch in node

but I dont think anyone should use it over normal client...

* target locking

* fixed post-rebasing issues
2023-09-07 13:30:04 +01:00

60 lines
1.3 KiB
JavaScript

import crypto from 'node:crypto';
import fs from 'node:fs';
import { TextDecoder, TextEncoder } from 'node:util';
import setGlobalVars from 'indexeddbshim';
import WebSocket from 'ws';
import fetch, {
Headers,
Request,
Response,
} from 'node-fetch'
if (!globalThis.fs) {
globalThis.fs = fs
}
if (!globalThis.process) {
globalThis.process = process
}
if (!globalThis.crypto) {
globalThis.crypto = {
getRandomValues(b) {
crypto.randomFillSync(b);
},
}
}
if (!globalThis.performance) {
globalThis.performance = {
now() {
const [sec, nsec] = process.hrtime();
return sec * 1000 + nsec / 1000000;
},
};
}
if (!globalThis.TextEncoder) {
globalThis.TextEncoder = TextEncoder
}
if (!globalThis.TextDecoder) {
globalThis.TextDecoder = TextDecoder
}
if (!globalThis.fetch) {
globalThis.fetch = fetch
globalThis.Headers = Headers
globalThis.Request = Request
globalThis.Response = Response
}
// checkOrigin:false is required to avoid SecurityError Cannot open
// an IndexedDB database from an opaque origin.
setGlobalVars(globalThis, {checkOrigin: false})
globalThis.WebSocket = WebSocket
// has to be loaded after all the polyfill action
import('@nymproject/mix-fetch-wasm/wasm_exec.js')