From 6a29fd83b650759fc655d5dc999f32b3afa758b0 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Wed, 2 Apr 2025 19:21:38 +0200 Subject: [PATCH] transform server to a proper esm module --- bin/{callback.cjs => callback.js} | 12 +++++----- bin/{server.cjs => server.js} | 10 ++++----- bin/{utils.cjs => utils.js} | 37 +++++++++++++------------------ rollup.config.mjs | 30 ++++++++++--------------- 4 files changed, 38 insertions(+), 51 deletions(-) rename bin/{callback.cjs => callback.js} (87%) rename bin/{server.cjs => server.js} (83%) rename bin/{utils.cjs => utils.js} (90%) diff --git a/bin/callback.cjs b/bin/callback.js similarity index 87% rename from bin/callback.cjs rename to bin/callback.js index b41c9a5..380ccad 100644 --- a/bin/callback.cjs +++ b/bin/callback.js @@ -1,18 +1,18 @@ -const http = require('http') -const number = require('lib0/number') +import http from 'http' +import * as number from 'lib0/number' const CALLBACK_URL = process.env.CALLBACK_URL ? new URL(process.env.CALLBACK_URL) : null 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 {import('./utils.cjs').WSSharedDoc} doc + * @param {import('./utils.js').WSSharedDoc} doc */ -exports.callbackHandler = (update, origin, doc) => { +export const callbackHandler = (update, origin, doc) => { const room = doc.name const dataToSend = { room, @@ -63,7 +63,7 @@ const callbackRequest = (url, timeout, data) => { /** * @param {string} objName * @param {string} objType - * @param {import('./utils.cjs').WSSharedDoc} doc + * @param {import('./utils.js').WSSharedDoc} doc */ const getContent = (objName, objType, doc) => { switch (objType) { diff --git a/bin/server.cjs b/bin/server.js similarity index 83% rename from bin/server.cjs rename to bin/server.js index ec5f4d8..c482474 100755 --- a/bin/server.cjs +++ b/bin/server.js @@ -1,11 +1,11 @@ #!/usr/bin/env node -const WebSocket = require('ws') -const http = require('http') -const number = require('lib0/number') -const wss = new WebSocket.Server({ noServer: true }) -const setupWSConnection = require('./utils.cjs').setupWSConnection +import WebSocket from 'ws' +import http from 'http' +import * as number from 'lib0/number' +import { setupWSConnection } from './utils.js' +const wss = new WebSocket.Server({ noServer: true }) const host = process.env.HOST || 'localhost' const port = number.parseInt(process.env.PORT || '1234') diff --git a/bin/utils.cjs b/bin/utils.js similarity index 90% rename from bin/utils.cjs rename to bin/utils.js index 61abc58..f0eca7a 100644 --- a/bin/utils.cjs +++ b/bin/utils.js @@ -1,15 +1,14 @@ -const Y = require('yjs') -const syncProtocol = require('y-protocols/sync') -const awarenessProtocol = require('y-protocols/awareness') +import * as Y from 'yjs' +import * as syncProtocol from 'y-protocols/sync' +import * as awarenessProtocol from 'y-protocols/awareness' -const encoding = require('lib0/encoding') -const decoding = require('lib0/decoding') -const map = require('lib0/map') +import * as encoding from 'lib0/encoding' +import * as decoding from 'lib0/decoding' +import * as map from 'lib0/map' -const debounce = require('lodash.debounce') +import debounce from 'lodash.debounce' -const callbackHandler = require('./callback.cjs').callbackHandler -const isCallbackSet = require('./callback.cjs').isCallbackSet +import { callbackHandler, isCallbackSet } from './callback.js' const CALLBACK_DEBOUNCE_WAIT = parseInt(process.env.CALLBACK_DEBOUNCE_WAIT || '2000') const CALLBACK_DEBOUNCE_MAXWAIT = parseInt(process.env.CALLBACK_DEBOUNCE_MAXWAIT || '10000') @@ -50,7 +49,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_ } @@ -58,14 +57,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 @@ -96,11 +93,11 @@ let contentInitializor = _ydoc => Promise.resolve() * * @param {(ydoc: Y.Doc) => Promise} f */ -exports.setContentInitializor = (f) => { +export const setContentInitializor = (f) => { contentInitializor = f } -class WSSharedDoc extends Y.Doc { +export class WSSharedDoc extends Y.Doc { /** * @param {string} name */ @@ -152,8 +149,6 @@ class WSSharedDoc extends Y.Doc { } } -exports.WSSharedDoc = WSSharedDoc - /** * Gets a Y.Doc by name, whether in memory or on disk * @@ -161,7 +156,7 @@ exports.WSSharedDoc = WSSharedDoc * @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) { @@ -171,8 +166,6 @@ const getYDoc = (docname, gc = true) => map.setIfUndefined(docs, docname, () => return doc }) -exports.getYDoc = getYDoc - /** * @param {any} conn * @param {WSSharedDoc} doc @@ -254,7 +247,7 @@ const pingTimeout = 30000 * @param {import('http').IncomingMessage} 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/rollup.config.mjs b/rollup.config.mjs index 966c61d..8302a5e 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -1,18 +1,12 @@ -export default { - input: './src/y-websocket.js', - external: id => /^(lib0|yjs|y-protocols)/.test(id), - output: [{ - name: 'y-websocket', - file: 'dist/y-websocket.cjs', - format: 'cjs', - sourcemap: true, - paths: path => { - if (/^lib0\//.test(path)) { - return `lib0/dist${path.slice(4)}.cjs` - } else if (/^y-protocols\//.test(path)) { - return `y-protocols/dist${path.slice(11)}.cjs` - } - return path - } - }] -} + export default [{ + input: ['./src/y-websocket.js', './bin/server.js', './bin/utils.js'], + external: id => /^(lib0|yjs|y-protocols|ws|lodash\.debounce|http)/.test(id), + output: [{ + dir: 'dist', + format: 'cjs', + sourcemap: true, + entryFileNames: '[name].cjs', + chunkFileNames: '[name]-[hash].cjs' + }] + } +]