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

V2 branch - Transformed server to a proper ESM module #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions bin/callback.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
const http = require('http')
import { WSSharedDoc } from './utils.js' // eslint-disable-line

import * as Y from 'yjs'
import http from 'http'

const CALLBACK_URL = process.env.CALLBACK_URL ? new URL(process.env.CALLBACK_URL) : null
const CALLBACK_TIMEOUT = process.env.CALLBACK_TIMEOUT || 5000
const CALLBACK_TIMEOUT = Number.parseInt(process.env.CALLBACK_TIMEOUT || '5000')
const CALLBACK_OBJECTS = process.env.CALLBACK_OBJECTS ? JSON.parse(process.env.CALLBACK_OBJECTS) : {}

exports.isCallbackSet = !!CALLBACK_URL
export const isCallbackSet = !!CALLBACK_URL

/**
* @param {Uint8Array} update
* @param {any} origin
* @param {WSSharedDoc} doc
*/
exports.callbackHandler = (update, origin, doc) => {
export const callbackHandler = (update, origin, doc) => {
const room = doc.name
const dataToSend = {
room: room,
Expand Down Expand Up @@ -70,7 +73,7 @@ const getContent = (objName, objType, doc) => {
case 'Map': return doc.getMap(objName)
case 'Text': return doc.getText(objName)
case 'XmlFragment': return doc.getXmlFragment(objName)
case 'XmlElement': return doc.getXmlElement(objName)
case 'XmlElement': return doc.get(objName, Y.XmlElement)
default : return {}
}
}
11 changes: 6 additions & 5 deletions bin/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
/**
* @type {any}
*/
const WebSocket = require('ws')
const http = require('http')
const wss = new WebSocket.Server({ noServer: true })
const setupWSConnection = require('./utils.js').setupWSConnection
import { WebSocketServer } from 'ws'
import http from 'http'
import { setupWSConnection } from './utils.js'

const wss = new WebSocketServer({ noServer: true })

const host = process.env.HOST || 'localhost'
const port = process.env.PORT || 1234
Expand All @@ -26,7 +27,7 @@ server.on('upgrade', (request, socket, head) => {
const handleAuth = ws => {
wss.emit('connection', ws, request)
}
wss.handleUpgrade(request, socket, head, handleAuth)
wss.handleUpgrade(request, /** @type {any} */ (socket), head, handleAuth)
})

server.listen(port, () => {
Expand Down
37 changes: 16 additions & 21 deletions bin/utils.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const Y = require('yjs')
const syncProtocol = require('y-protocols/dist/sync.cjs')
const awarenessProtocol = require('y-protocols/dist/awareness.cjs')
import * as Y from 'yjs'
import * as syncProtocol from 'y-protocols/sync'
import * as awarenessProtocol from 'y-protocols/awareness'

const encoding = require('lib0/dist/encoding.cjs')
const decoding = require('lib0/dist/decoding.cjs')
const mutex = require('lib0/dist/mutex.cjs')
const map = require('lib0/dist/map.cjs')
import * as encoding from 'lib0/encoding'
import * as decoding from 'lib0/decoding'
import * as mutex from 'lib0/mutex'
import * as map from 'lib0/map'

const debounce = require('lodash.debounce')
import debounce from 'lodash.debounce'

const callbackHandler = require('./callback.js').callbackHandler
const isCallbackSet = require('./callback.js').isCallbackSet
import { callbackHandler, isCallbackSet } from './callback.js'
import { LeveldbPersistence } from 'y-leveldb'

const CALLBACK_DEBOUNCE_WAIT = parseInt(process.env.CALLBACK_DEBOUNCE_WAIT) || 2000
const CALLBACK_DEBOUNCE_MAXWAIT = parseInt(process.env.CALLBACK_DEBOUNCE_MAXWAIT) || 10000
Expand All @@ -30,7 +30,6 @@ let persistence = null
if (typeof persistenceDir === 'string') {
console.info('Persisting documents to "' + persistenceDir + '"')
// @ts-ignore
const LeveldbPersistence = require('y-leveldb').LeveldbPersistence
const ldb = new LeveldbPersistence(persistenceDir)
persistence = {
provider: ldb,
Expand All @@ -51,22 +50,20 @@ if (typeof persistenceDir === 'string') {
* @param {{bindState: function(string,WSSharedDoc):void,
* writeState:function(string,WSSharedDoc):Promise<any>,provider:any}|null} persistence_
*/
exports.setPersistence = persistence_ => {
export const setPersistence = persistence_ => {
persistence = persistence_
}

/**
* @return {null|{bindState: function(string,WSSharedDoc):void,
* writeState:function(string,WSSharedDoc):Promise<any>}|null} used persistence layer
*/
exports.getPersistence = () => persistence
export const getPersistence = () => persistence

/**
* @type {Map<string,WSSharedDoc>}
*/
const docs = new Map()
// exporting docs so that others can use it
exports.docs = docs
export const docs = new Map()

const messageSync = 0
const messageAwareness = 1
Expand All @@ -85,7 +82,7 @@ const updateHandler = (update, origin, doc) => {
doc.conns.forEach((_, conn) => send(doc, conn, message))
}

class WSSharedDoc extends Y.Doc {
export class WSSharedDoc extends Y.Doc {
/**
* @param {string} name
*/
Expand Down Expand Up @@ -144,7 +141,7 @@ class WSSharedDoc extends Y.Doc {
* @param {boolean} gc - whether to allow gc on the doc (applies only when created)
* @return {WSSharedDoc}
*/
const getYDoc = (docname, gc = true) => map.setIfUndefined(docs, docname, () => {
export const getYDoc = (docname, gc = true) => map.setIfUndefined(docs, docname, () => {
const doc = new WSSharedDoc(docname)
doc.gc = gc
if (persistence !== null) {
Expand All @@ -154,8 +151,6 @@ const getYDoc = (docname, gc = true) => map.setIfUndefined(docs, docname, () =>
return doc
})

exports.getYDoc = getYDoc

/**
* @param {any} conn
* @param {WSSharedDoc} doc
Expand Down Expand Up @@ -232,7 +227,7 @@ const pingTimeout = 30000
* @param {any} req
* @param {any} opts
*/
exports.setupWSConnection = (conn, req, { docName = req.url.slice(1).split('?')[0], gc = true } = {}) => {
export const 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)
Expand Down
42 changes: 16 additions & 26 deletions package-lock.json

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

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "y-websocket",
"version": "1.3.17",
"description": "Websockets provider for Yjs",
"type": "module",
"main": "./dist/y-websocket.cjs",
"module": "./src/y-websocket.js",
"types": "./dist/src/y-websocket.d.ts",
Expand Down Expand Up @@ -54,22 +55,22 @@
]
},
"dependencies": {
"lib0": "^0.2.42",
"lib0": "^0.2.43",
"lodash.debounce": "^4.0.8",
"ws": "^8.3.0",
"y-protocols": "^1.0.5"
},
"devDependencies": {
"rollup": "^1.32.1",
"rollup-cli": "^1.0.9",
"standard": "^12.0.1",
"typescript": "^3.9.9",
"yjs": "^13.5.0"
"typescript": "^3.9.10",
"yjs": "^13.5.22"
},
"peerDependencies": {
"yjs": "^13.5.6"
},
"optionalDependencies": {
"ws": "^6.2.1",
"y-leveldb": "^0.1.0"
"y-leveldb": "^0.1.1"
}
}