Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime error during dev mode with sockjs-client dependency #8518

Closed
7 tasks done
pascalgn opened this issue Jun 9, 2022 · 1 comment
Closed
7 tasks done

Runtime error during dev mode with sockjs-client dependency #8518

pascalgn opened this issue Jun 9, 2022 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@pascalgn
Copy link

pascalgn commented Jun 9, 2022

Describe the bug

When using sockjs-client in dev mode with vite, I get the following error:

TypeError: crypto.randomBytes is not a function
    at Object.string (random.js:11:1)
    at Object.unloadAdd (event.js:39:1)
    at XHRCorsObject.AbstractXHRObject._start (abstract-xhr.js:48:1)
    at abstract-xhr.js:21:1

(Same error, but different issue: sockjs/sockjs-client#575)

In production mode (e.g. vite preview) it works without problems.

I think this is a bit similar to #8141, because my guess is that it has to do with the browser field, which is set in sockjs-client as:

  "browser": {
    "./lib/transport/driver/websocket.js": "./lib/transport/browser/websocket.js",
    "eventsource": "./lib/transport/browser/eventsource.js",
    "./lib/transport/driver/xhr.js": "./lib/transport/browser/abstract-xhr.js",
    "crypto": "./lib/utils/browser-crypto.js",
    "events": "./lib/event/emitter.js"
  }

When I change my vite config like this, it works, but I'm not sure if that is a good idea

export default defineConfig({
  resolve: {
    alias: {
      crypto: "./node_modules/sockjs-client/lib/utils/browser-crypto.js"
    }
  }
})

Any ideas?

Reproduction

https://stackblitz.com/edit/vitejs-vite-8purpq

System Info

  System:
    OS: macOS 12.2.1
    CPU: (8) x64 Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
    Memory: 2.13 GB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 14.19.1 - /usr/local/opt/node@14/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 6.14.16 - /usr/local/opt/node@14/bin/npm
  Browsers:
    Chrome: 102.0.5005.61
    Firefox: 99.0.1
    Safari: 15.3
  npmPackages:
    @vitejs/plugin-legacy: ^1.7.1 => 1.7.1 
    @vitejs/plugin-react: ^1.0.7 => 1.2.0 
    vite: ^2.8.0 => 2.8.4 

Used Package Manager

yarn

Build output

With the above workaround, the referenced file in the build looks fine:

import {
  __commonJS
} from "./chunk-6DBBYCL5.js";

// node_modules/sockjs-client/lib/utils/browser-crypto.js
var require_browser_crypto = __commonJS({
  "node_modules/sockjs-client/lib/utils/browser-crypto.js"(exports, module) {
    "use strict";
    if (global.crypto && global.crypto.getRandomValues) {
      module.exports.randomBytes = function(length) {
        var bytes = new Uint8Array(length);
        global.crypto.getRandomValues(bytes);
        return bytes;
      };
    } else {
      module.exports.randomBytes = function(length) {
        var bytes = new Array(length);
        for (var i = 0; i < length; i++) {
          bytes[i] = Math.floor(Math.random() * 256);
        }
        return bytes;
      };
    }
  }
});

export {
  require_browser_crypto
};
//# sourceMappingURL=chunk-DMEIAON6.js.map

Without the fix, the file looks empty (and therefore, crypto.randomBytes is undefined)

import {
  __esm,
  __export
} from "./chunk-6DBBYCL5.js";

// browser-external:crypto
var crypto_exports = {};
__export(crypto_exports, {
  default: () => crypto_default
});
var crypto_default;
var init_crypto = __esm({
  "browser-external:crypto"() {
    crypto_default = new Proxy({}, {
      get() {
        throw new Error('Module "crypto" has been externalized for browser compatibility and cannot be accessed in client code.');
      }
    });
  }
});

export {
  crypto_exports,
  init_crypto
};
//# sourceMappingURL=chunk-VW2V5EWT.js.map

Validations

@sapphi-red
Copy link
Member

Closing as it is a duplicate of #7301.

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Jun 9, 2022
@sapphi-red sapphi-red added duplicate This issue or pull request already exists and removed pending triage labels Jun 9, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jun 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants