From 01b9a6060a368d55322cf18dfebcfcb3461173fb Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Tue, 30 Nov 2021 14:58:26 +0100 Subject: [PATCH] transformed server to a proper esm module --- bin/callback.js | 13 ++++++++----- bin/server.js | 11 ++++++----- bin/utils.js | 37 ++++++++++++++++--------------------- package-lock.json | 42 ++++++++++++++++-------------------------- package.json | 11 ++++++----- 5 files changed, 52 insertions(+), 62 deletions(-) diff --git a/bin/callback.js b/bin/callback.js index b0e49e5c..d7a7756f 100644 --- a/bin/callback.js +++ b/bin/callback.js @@ -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, @@ -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 {} } } diff --git a/bin/server.js b/bin/server.js index cb3508a7..a59f8726 100755 --- a/bin/server.js +++ b/bin/server.js @@ -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 @@ -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, () => { diff --git a/bin/utils.js b/bin/utils.js index 0c9a7b7e..e9d60c70 100644 --- a/bin/utils.js +++ b/bin/utils.js @@ -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 @@ -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, @@ -51,7 +50,7 @@ if (typeof persistenceDir === 'string') { * @param {{bindState: function(string,WSSharedDoc):void, * writeState:function(string,WSSharedDoc):Promise,provider:any}|null} persistence_ */ -exports.setPersistence = persistence_ => { +export const setPersistence = persistence_ => { persistence = persistence_ } @@ -59,14 +58,12 @@ exports.setPersistence = persistence_ => { * @return {null|{bindState: function(string,WSSharedDoc):void, * writeState:function(string,WSSharedDoc):Promise}|null} used persistence layer */ -exports.getPersistence = () => persistence +export const getPersistence = () => persistence /** * @type {Map} */ -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 @@ -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 */ @@ -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) { @@ -154,8 +151,6 @@ const getYDoc = (docname, gc = true) => map.setIfUndefined(docs, docname, () => return doc }) -exports.getYDoc = getYDoc - /** * @param {any} conn * @param {WSSharedDoc} doc @@ -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) diff --git a/package-lock.json b/package-lock.json index 1aa1f260..c1061cf5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -99,12 +99,6 @@ "is-string": "^1.0.5" } }, - "async-limiter": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz", - "integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==", - "optional": true - }, "babel-code-frame": { "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", @@ -1233,9 +1227,9 @@ } }, "lib0": { - "version": "0.2.42", - "resolved": "https://registry.npmjs.org/lib0/-/lib0-0.2.42.tgz", - "integrity": "sha512-8BNM4MiokEKzMvSxTOC3gnCBisJH+jL67CnSnqzHv3jli3pUvGC8wz+0DQ2YvGr4wVQdb2R2uNNPw9LEpVvJ4Q==", + "version": "0.2.43", + "resolved": "https://registry.npmjs.org/lib0/-/lib0-0.2.43.tgz", + "integrity": "sha512-MJ1KLoz5p3gljIUBfdjjNuL/wlWHHK6+DrcIRhzSRLvtAu1XNdRtRGATYM51KSTI0P2nxJZFQM8rwCH6ga9KUw==", "requires": { "isomorphic.js": "^0.2.4" } @@ -1989,9 +1983,9 @@ } }, "typescript": { - "version": "3.9.9", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.9.tgz", - "integrity": "sha512-kdMjTiekY+z/ubJCATUPlRDl39vXYiMV9iyeMuEuXZh2we6zz80uovNN2WlAxmmdE/Z/YQe+EbOEXB5RHEED3w==", + "version": "3.9.10", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.10.tgz", + "integrity": "sha512-w6fIxVE/H1PkLKcCPsFqKE7Kv7QUwhU8qQY2MueZXWx5cPZdwFupLgKK3vntcK98BtNHZtAF4LA/yl2a7k8R6Q==", "dev": true }, "unbox-primitive": { @@ -2081,13 +2075,9 @@ } }, "ws": { - "version": "6.2.1", - "resolved": "https://registry.npmjs.org/ws/-/ws-6.2.1.tgz", - "integrity": "sha512-GIyAXC2cB7LjvpgMt9EKS2ldqr0MTrORaleiOno6TweZ6r3TKtoFQWay/2PceJ3RuBasOHzXNn5Lrw1X0bEjqA==", - "optional": true, - "requires": { - "async-limiter": "~1.0.0" - } + "version": "8.3.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.3.0.tgz", + "integrity": "sha512-Gs5EZtpqZzLvmIM59w4igITU57lrtYVFneaa434VROv4thzJyV6UjIL3D42lslWlI+D4KzLYnxSwtfuiO79sNw==" }, "xtend": { "version": "4.0.2", @@ -2095,9 +2085,9 @@ "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==" }, "y-leveldb": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/y-leveldb/-/y-leveldb-0.1.0.tgz", - "integrity": "sha512-sMuitVrsAUNh+0b66I42nAuW3lCmez171uP4k0ePcTAJ+c+Iw9w4Yq3wwiyrDMFXBEyQSjSF86Inc23wEvWnxw==", + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/y-leveldb/-/y-leveldb-0.1.1.tgz", + "integrity": "sha512-L8Q0MQmxCQ0qWIOuPzLbWn95TNhrCI7M6LaHnilU4I2IX08e4Dmfg5Tgy4JZ3tnl2aiuZyDOJplHl/msIB/IsA==", "optional": true, "requires": { "level": "^6.0.1", @@ -2113,12 +2103,12 @@ } }, "yjs": { - "version": "13.5.6", - "resolved": "https://registry.npmjs.org/yjs/-/yjs-13.5.6.tgz", - "integrity": "sha512-0ebPpLB/zizJbWaFUDRarWbXiXYD0OMDOCa8ZqkVVWQzeIoMRbmbNwB3suZ9YwD0bV4Su9RLn8M/bvGzIwX3hA==", + "version": "13.5.22", + "resolved": "https://registry.npmjs.org/yjs/-/yjs-13.5.22.tgz", + "integrity": "sha512-qxsVlu/E2dLUUtJyhDbwkhrw1gWbdE+UWzVI2VEeY6G1M8TYI51VvXSoPC/4QQMNIyEdsCOW2cWxPUyoCH43gw==", "dev": true, "requires": { - "lib0": "^0.2.41" + "lib0": "^0.2.43" } } } diff --git a/package.json b/package.json index 2cc39af9..8ed3b6fc 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } }