* @returns {EXPECTED_ANY} env
()
| 774 | * @returns {EXPECTED_ANY} env |
| 775 | */ |
| 776 | setupEnv() { |
| 777 | if (this.jsDom()) { |
| 778 | const outputDirectory = this.outputDirectory; |
| 779 | |
| 780 | const FakeDocument = require("../../helpers/FakeDocument"); |
| 781 | const createFakeWorker = require("../../helpers/createFakeWorker"); |
| 782 | const EventSource = require("../../helpers/EventSourceForNode"); |
| 783 | |
| 784 | const document = new FakeDocument(outputDirectory); |
| 785 | if (this.testConfig.evaluateScriptOnAttached) { |
| 786 | document.onScript = (/** @type {string | undefined} */ src) => { |
| 787 | this.require( |
| 788 | outputDirectory, |
| 789 | urlToRelativePath(/** @type {string} */ (src)) |
| 790 | ); |
| 791 | }; |
| 792 | } |
| 793 | const fetch = async (/** @type {string | URL} */ url) => { |
| 794 | try { |
| 795 | // universal targets pass a `URL` (often `file:`) to fetch |
| 796 | const filePath = |
| 797 | url instanceof URL || String(url).startsWith("file:") |
| 798 | ? fileURLToPath(url) |
| 799 | : urlToPath(String(url), this.outputDirectory); |
| 800 | const buffer = await new Promise((resolve, reject) => { |
| 801 | fs.readFile(filePath, (err, b) => (err ? reject(err) : resolve(b))); |
| 802 | }); |
| 803 | // `instantiateStreaming` needs a real Response with a wasm mime type |
| 804 | if (typeof Response !== "undefined") { |
| 805 | const contentType = filePath.endsWith(".wasm") |
| 806 | ? "application/wasm" |
| 807 | : filePath.endsWith(".json") |
| 808 | ? "application/json" |
| 809 | : "text/plain"; |
| 810 | return new Response(buffer, { |
| 811 | status: 200, |
| 812 | headers: { "Content-Type": contentType } |
| 813 | }); |
| 814 | } |
| 815 | return { |
| 816 | status: 200, |
| 817 | ok: true, |
| 818 | headers: { get: () => "application/wasm" }, |
| 819 | arrayBuffer: async () => buffer, |
| 820 | text: async () => buffer.toString("utf8"), |
| 821 | json: async () => JSON.parse(buffer.toString("utf8")) |
| 822 | }; |
| 823 | } catch (/** @type {EXPECTED_ANY} */ err) { |
| 824 | if (err.code === "ENOENT") { |
| 825 | return typeof Response !== "undefined" |
| 826 | ? new Response(null, { status: 404 }) |
| 827 | : { |
| 828 | status: 404, |
| 829 | ok: false |
| 830 | }; |
| 831 | } |
| 832 | throw err; |
| 833 | } |
no test coverage detected