Skip to content

Commit

Permalink
address nodejs unit testing issue
Browse files Browse the repository at this point in the history
mocha tests started failing as of node 20.10.0, likely due to this change:
nodejs/node#49936

Error was:
node:internal/deps/undici/undici:11730
    Error.captureStackTrace(err, this);
          ^

TypeError: Failed to parse URL from js/zlib-1.2.13.wasm
    at Object.fetch (node:internal/deps/undici/undici:11730:11)
    at async initialize (/home/runner/work/PrivateBin/PrivateBin/js/zlib-1.2.13.js:31:26) {
  [cause]: TypeError: Invalid URL: js/zlib-1.2.13.wasm
      at new URLImpl (/home/runner/work/PrivateBin/PrivateBin/js/node_modules/jsdom-url/node_modules/whatwg-url/lib/URL-impl.js:21:13)
      at new URLImplCore (/home/runner/work/PrivateBin/PrivateBin/js/node_modules/jsdom-url/lib/URLImpl.js:18:9)
      at new URLCore (/home/runner/work/PrivateBin/PrivateBin/js/node_modules/jsdom-url/lib/URL.js:28:9)
      at Object.construct (/home/runner/work/PrivateBin/PrivateBin/js/node_modules/class-proxy/index.js:18:23)
      at new Request (node:internal/deps/undici/undici:5270:25)
      at fetch (node:internal/deps/undici/undici:9508:25)
      at Object.fetch (node:internal/deps/undici/undici:11728:18)
      at fetch (node:internal/process/pre_execution:314:27)
      at initialize (/home/runner/work/PrivateBin/PrivateBin/js/zlib-1.2.13.js:31:32)
      at Object.<anonymous> (/home/runner/work/PrivateBin/PrivateBin/js/zlib-1.2.13.js:145:17)
      at Object.<anonymous> (/home/runner/work/PrivateBin/PrivateBin/js/zlib-1.2.13.js:146:4)
[...]

Notice that the error occurs on line 31, meaning that fetch is not
undefined anymore. Node works on supporting fetch, which would make our
workaround using fs.readFileSync obsolete, but it (or rather the undici
library) currently doesn't support relative URLs.
  • Loading branch information
elrido committed Dec 3, 2023
1 parent aa1a44e commit b150450
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion js/zlib-1.2.13.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
};

let buff;
if (typeof fetch === 'undefined') {
if (typeof fs === 'object') {
buff = fs.readFileSync('zlib-1.2.13.wasm');
} else {
const resp = await fetch('js/zlib-1.2.13.wasm');
Expand Down

2 comments on commit b150450

@rugk
Copy link
Member

@rugk rugk commented on b150450 Dec 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be upstreamed to the zlib lib wherever we got that from?

@elrido
Copy link
Contributor Author

@elrido elrido commented on b150450 Dec 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, two things:

  1. the upstream hasn't been updated since 2017 - I had raised the last two zlib updates as PRs over there, but they didn't get merged. I don't mind that so much, the project mostly provides a convenient build env to compile the unchanged zlib C-sources into wasm - as long as zlib continues being maintained and compiles to wasm and doesn't change their C-ABI, we should be fine.
  2. the provided sample loader doesn't have this issue, since it is written only for node, using the fs object, not the fetch function used in browsers - ours is modified to work in both environments and not pollute the global namespace (yes, could nowadays be solved more elegantly using ES6 modules).

Please sign in to comment.