Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: yjs/y-websocket
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.0.2
Choose a base ref
...
head repository: yjs/y-websocket
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.0.3
Choose a head ref
  • 8 commits
  • 7 files changed
  • 3 contributors

Commits on Mar 26, 2024

  1. update

    dmonad committed Mar 26, 2024

    Unverified

    This user has not yet uploaded their public signing key.
    Copy the full SHA
    af5c36d View commit details

Commits on Mar 27, 2024

  1. add tips for handling auth and add typings for ws

    dmonad committed Mar 27, 2024
    Copy the full SHA
    7945a5b View commit details

Commits on Apr 19, 2024

  1. Update callback.cjs

    Incorrect content-length in callback.js when body contains non-ascii characters
    chenmins authored Apr 19, 2024
    Copy the full SHA
    01a6e80 View commit details
  2. Merge pull request #181 from chenmins/master

    Incorrect content-length in callback.js when body contains non-ascii characters
    dmonad authored Apr 19, 2024
    Copy the full SHA
    12608bd View commit details

Commits on Apr 27, 2024

  1. Copy the full SHA
    9e2dab6 View commit details

Commits on May 9, 2024

  1. make params configurable

    dmonad committed May 9, 2024
    Copy the full SHA
    d8f1e6b View commit details
  2. add docs for url parameters

    dmonad committed May 9, 2024
    Copy the full SHA
    ce95ba8 View commit details
  3. 2.0.3

    dmonad committed May 9, 2024
    Copy the full SHA
    630e08e View commit details
Showing with 63 additions and 26 deletions.
  1. +5 −1 README.md
  2. +1 −1 bin/callback.cjs
  3. +4 −9 bin/server.cjs
  4. +7 −7 bin/utils.cjs
  5. +28 −2 package-lock.json
  6. +3 −1 package.json
  7. +15 −5 src/y-websocket.js
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -82,7 +82,7 @@ import { WebsocketProvider } from 'y-websocket'
wsOpts = {
// Set this to `false` if you want to connect manually using wsProvider.connect()
connect: true,
// Specify a query-string that will be url-encoded and attached to the `serverUrl`
// Specify a query-string / url parameters that will be url-encoded and attached to the `serverUrl`
// I.e. params = { auth: "bearer" } will be transformed to "?auth=bearer"
params: {}, // Object<string,string>
// You may polyill the Websocket object (https://developer.mozilla.org/en-US/docs/Web/API/WebSocket).
@@ -106,6 +106,10 @@ wsOpts = {
<dd>True if this instance is currently communicating to other browser-windows via BroadcastChannel.</dd>
<b><code>wsProvider.synced: boolean</code></b>
<dd>True if this instance is currently connected and synced with the server.</dd>
<b><code>wsProvider.params : boolean</code></b>
<dd>The specified url parameters. This can be safely updated, the new values
will be used when a new connction is established. If this contains an
auth token, it should be updated regularly.</dd>
<b><code>wsProvider.disconnect()</code></b>
<dd>Disconnect from the server and don't try to reconnect.</dd>
<b><code>wsProvider.connect()</code></b>
2 changes: 1 addition & 1 deletion bin/callback.cjs
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ const callbackRequest = (url, timeout, data) => {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
'Content-Length': Buffer.byteLength(data)
}
}
const req = http.request(options)
13 changes: 4 additions & 9 deletions bin/server.cjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#!/usr/bin/env node

/**
* @type {any}
*/
const WebSocket = require('ws')
const http = require('http')
const number = require('lib0/number')
@@ -21,14 +18,12 @@ wss.on('connection', setupWSConnection)

server.on('upgrade', (request, socket, head) => {
// You may check auth of request here..
// Call `wss.HandleUpgrade` *after* you checked whether the client has access
// (e.g. by checking cookies, or url parameters).
// See https://github.com/websockets/ws#client-authentication
/**
* @param {any} ws
*/
const handleAuth = ws => {
wss.handleUpgrade(request, socket, head, /** @param {any} ws */ ws => {
wss.emit('connection', ws, request)
}
wss.handleUpgrade(request, socket, head, handleAuth)
})
})

server.listen(port, host, () => {
14 changes: 7 additions & 7 deletions bin/utils.cjs
Original file line number Diff line number Diff line change
@@ -142,11 +142,11 @@ class WSSharedDoc extends Y.Doc {
this.awareness.on('update', awarenessChangeHandler)
this.on('update', /** @type {any} */ (updateHandler))
if (isCallbackSet) {
this.on('update', debounce(
this.on('update', /** @type {any} */ (debounce(
callbackHandler,
CALLBACK_DEBOUNCE_WAIT,
{ maxWait: CALLBACK_DEBOUNCE_MAXWAIT }
))
)))
}
this.whenInitialized = contentInitializor(this)
}
@@ -233,15 +233,15 @@ const closeConn = (doc, conn) => {

/**
* @param {WSSharedDoc} doc
* @param {any} conn
* @param {import('ws').WebSocket} conn
* @param {Uint8Array} m
*/
const send = (doc, conn, m) => {
if (conn.readyState !== wsReadyStateConnecting && conn.readyState !== wsReadyStateOpen) {
closeConn(doc, conn)
}
try {
conn.send(m, /** @param {any} err */ err => { err != null && closeConn(doc, conn) })
conn.send(m, {}, err => { err != null && closeConn(doc, conn) })
} catch (e) {
closeConn(doc, conn)
}
@@ -250,11 +250,11 @@ const send = (doc, conn, m) => {
const pingTimeout = 30000

/**
* @param {any} conn
* @param {any} req
* @param {import('ws').WebSocket} conn
* @param {import('http').IncomingMessage} req
* @param {any} opts
*/
exports.setupWSConnection = (conn, req, { docName = req.url.slice(1).split('?')[0], gc = true } = {}) => {
exports.setupWSConnection = (conn, req, { docName = (req.url || '').slice(1).split('?')[0], gc = true } = {}) => {
conn.binaryType = 'arraybuffer'
// get doc, initialize if it does not exist yet
const doc = getYDoc(docName, gc)
30 changes: 28 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "y-websocket",
"version": "2.0.2",
"version": "2.0.3",
"description": "Websockets provider for Yjs",
"main": "./dist/y-websocket.cjs",
"module": "./src/y-websocket.js",
@@ -64,7 +64,9 @@
"y-protocols": "^1.0.5"
},
"devDependencies": {
"@types/lodash.debounce": "^4.0.9",
"@types/node": "^18.15.0",
"@types/ws": "^8.5.10",
"rollup": "^3.19.1",
"standard": "^12.0.1",
"typescript": "^4.9.5",
20 changes: 15 additions & 5 deletions src/y-websocket.js
Original file line number Diff line number Diff line change
@@ -245,7 +245,7 @@ export class WebsocketProvider extends Observable {
* @param {object} opts
* @param {boolean} [opts.connect]
* @param {awarenessProtocol.Awareness} [opts.awareness]
* @param {Object<string,string>} [opts.params]
* @param {Object<string,string>} [opts.params] specify url parameters
* @param {typeof WebSocket} [opts.WebSocketPolyfill] Optionall provide a WebSocket polyfill
* @param {number} [opts.resyncInterval] Request server state every `resyncInterval` milliseconds
* @param {number} [opts.maxBackoffTime] Maximum amount of time to wait before trying to reconnect (we try to reconnect using exponential backoff)
@@ -265,11 +265,15 @@ export class WebsocketProvider extends Observable {
while (serverUrl[serverUrl.length - 1] === '/') {
serverUrl = serverUrl.slice(0, serverUrl.length - 1)
}
const encodedParams = url.encodeQueryParams(params)
this.maxBackoffTime = maxBackoffTime
this.serverUrl = serverUrl
this.bcChannel = serverUrl + '/' + roomname
this.url = serverUrl + '/' + roomname +
(encodedParams.length === 0 ? '' : '?' + encodedParams)
this.maxBackoffTime = maxBackoffTime
/**
* The specified url parameters. This can be safely updated. The changed parameters will be used
* when a new connection is established.
* @type {Object<string,string>}
*/
this.params = params
this.roomname = roomname
this.doc = doc
this._WS = WebSocketPolyfill
@@ -378,6 +382,12 @@ export class WebsocketProvider extends Observable {
}
}

get url () {
const encodedParams = url.encodeQueryParams(this.params)
return this.serverUrl + '/' + this.roomname +
(encodedParams.length === 0 ? '' : '?' + encodedParams)
}

/**
* @type {boolean}
*/