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

Make y-websocket run on node.js so that websocket server for y-websocket can be tested #65

Closed
kapv89 opened this issue May 16, 2021 — with Huly for GitHub · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

Copy link

kapv89 commented May 16, 2021

Checklist

[ ] Are you reporting a bug? Use github issues for bug reports and feature requests. For general questions, please use https://discuss.yjs.dev/ [ ] Try to report your issue in the correct repository. Yjs consists of many modules. When in doubt, report it to https://github.com/yjs/yjs/issues/

Is your feature request related to a problem? Please describe. I wrote tests for a ws-server for y-websocket using lib0/testing (since I don't know any other testing-lib that works with esm). When I tried to run those tests, I got an error message regarding usage of window in y-websocket (from this line). These type of things prevent running y-websocket in a node.js environment and writing and running automated tests for it

Describe the solution you'd like y-websocket should be runnable on node.js. If some help is needed, then I can copy over the testing-code which tries to use y-websocket in node.js env from my private repo to a public repo, and that repo can be used to verify if y-websocket can be run on backend.

Describe alternatives you've considered not testing the ws server which y-websocket connects to is the only alternative, but it doesn't bring much confidence

Additional context test-code:

import * as t from 'lib0/testing.js';
import * as Y from 'yjs';
import faker from 'faker';
import WS from 'ws';
import {WebsocketProvider} from 'y-websocket';
import { nockCheckAuth, nockGetUpdates, nockPostUpdate, wsUrl } from './index.js';

export const testSingleDoc = async (tc: t.TestCase) => {
  const id = faker.datatype.uuid();
  const token = faker.lorem.word();
  const doc = new Y.Doc();
  const updates: string[] = []

  nockCheckAuth(token);
  nockGetUpdates(token, id, updates);
  nockPostUpdate(token, id, updates);
  const wsProvider = new WebsocketProvider(wsUrl(), id, doc, {params: {token}, WebSocketPolyfill: WS as any})

  const items = doc.getArray('items');
  items.push([faker.random.word()]);

  await new Promise<void>(resolve => setTimeout(resolve, 1000));
  wsProvider.destroy();
}

error-message

[1/1] app: single doc
  ReferenceError: window is not defined
      at new WebsocketProvider (/home/k/Projects/proj/yjs-proto/sw/crdt-ws/node_modules/y-websocket/src/y-websocket.js:280:5)
      at testSingleDoc (file:///home/k/Projects/proj/yjs-proto/sw/crdt-ws/src/__tests__/app.test.ts:17:22)
      at run (file:///home/k/Projects/proj/yjs-proto/sw/crdt-ws/node_modules/lib0/testing.js:158:17)
      at runTests (file:///home/k/Projects/proj/yjs-proto/sw/crdt-ws/node_modules/lib0/testing.js:552:27)
      at file:///home/k/Projects/proj/yjs-proto/sw/crdt-ws/src/__tests__/main.ts:8:11
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
Failure: single doc in 3.54ms
repeat: npm run test -- --filter "\[1/" 

> 1 test failed

Huly®: YJS-501

@kapv89 kapv89 added the enhancement New feature or request label May 16, 2021
@kapv89 kapv89 changed the title Make y-websocket run on node.js so that websocket server or y-websocket can be tested Make y-websocket run on node.js so that websocket server for y-websocket can be tested May 16, 2021
@dmonad dmonad closed this as completed in 010658f May 17, 2021
@dmonad
Copy link
Member

dmonad commented May 17, 2021

Fixed it in [email protected]

billiegoose added a commit to stoplightio/y-websocket that referenced this issue Sep 29, 2021
* Update README.md

* fix: Memory leak with old versions of yjs

Versions of yjs prior to `13.4.6` did not emit a `"destroy"` event when a doc is destroyed, meaning that doc's in the websocket servers memory are not correctly cleaned up.

* Update package-lock.json

* Extendable messageHanlers

* update deps

* 1.3.10

* remove node types from client code

* 1.3.11

* lint

* Add info about y-websocket-server to quick-start

* add on exit handler for Node.js environments

* 1.3.14

* Fix a `peerDependencies` typo in `package.json`

* fix typo

* Remove dependency on window - fixes yjs#65

* 1.3.12

* use condititional exports of dependencies

* 1.3.13

* set correct module type

* 1.3.14

* fix yjs#69 by reverting esm change

* 1.3.15

* Fix beforeunload memory leak

* 1.3.16

Co-authored-by: Kevin Jahns <[email protected]>
Co-authored-by: Tom Moor <[email protected]>
Co-authored-by: Kevin Jahns <[email protected]>
Co-authored-by: Hans Pagel <[email protected]>
Co-authored-by: Thomas Parisot <[email protected]>
Co-authored-by: Nemanja Tosic <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants