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

References to window object crash node scripts using WebsocketProvider #51

Closed
2 tasks done
cristiano-belloni opened this issue Jan 27, 2021 · 0 comments
Closed
2 tasks done
Assignees
Labels
bug Something isn't working

Comments

@cristiano-belloni
Copy link

Checklist

Describe the bug
When using WebsocketProvider in a node script, y-websocket.js and lib0 reference window, making the script crash respectively with:

ReferenceError: window is not defined
    at new WebsocketProvider (/.../node_modules/y-websocket/dist/y-websocket.cjs:267:5)
ReferenceError: addEventListener is not defined
    at new LocalStoragePolyfill (node_modules/lib0/dist/broadcastchannel-49d2cc56.cjs:30:5)
    at node_modules/lib0/dist/broadcastchannel-49d2cc56.cjs:51:16
    at Object.setIfUndefined (node_modules/lib0/dist/map-28a001c9.cjs:51:24)
    at getChannel (node_modules/lib0/dist/broadcastchannel-49d2cc56.cjs:49:7)
    at Object.subscribe (node_modules/lib0/dist/broadcastchannel-49d2cc56.cjs:68:32)
    at WebsocketProvider.connectBc (node_modules/y-websocket/dist/y-websocket.cjs:311:10)
    at WebsocketProvider.connect (node_modules/y-websocket/dist/y-websocket.cjs:362:12)
    at new WebsocketProvider (node_modules/y-websocket/dist/y-websocket.cjs:279:12)

To Reproduce
Steps to reproduce the behavior:

  1. Run a node script like:
const Y = require("yjs");
const { WebsocketProvider } = require("y-websocket");
const doc = new Y.Doc();

const wsProvider = new WebsocketProvider(
  "ws://localhost:1234",
  "test-room",
  doc,
  { WebSocketPolyfill: require("ws") }
);
  1. See error

Expected behavior
No error.

Environment Information

  • Node.js
  • "y-websocket": "^1.3.9",
  • "yjs": "^13.4.12"

Additional context

This code successfully works around the issue:

window = {
  addEventListener: () => null,
};
addEventListener = () => null;
@cristiano-belloni cristiano-belloni added the bug Something isn't working label Jan 27, 2021
dmonad added a commit that referenced this issue Jun 25, 2021
Add exit handler for Node.js environments, fix #51 #55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants