transform server to a proper esm module

This commit is contained in:
Kevin Jahns 2025-04-02 19:21:38 +02:00
parent 62a9aa1374
commit 6a29fd83b6
4 changed files with 38 additions and 51 deletions

View File

@ -1,18 +1,18 @@
const http = require('http') import http from 'http'
const number = require('lib0/number') import * as number from 'lib0/number'
const CALLBACK_URL = process.env.CALLBACK_URL ? new URL(process.env.CALLBACK_URL) : null 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_TIMEOUT = number.parseInt(process.env.CALLBACK_TIMEOUT || '5000')
const CALLBACK_OBJECTS = process.env.CALLBACK_OBJECTS ? JSON.parse(process.env.CALLBACK_OBJECTS) : {} 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 {Uint8Array} update
* @param {any} origin * @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 room = doc.name
const dataToSend = { const dataToSend = {
room, room,
@ -63,7 +63,7 @@ const callbackRequest = (url, timeout, data) => {
/** /**
* @param {string} objName * @param {string} objName
* @param {string} objType * @param {string} objType
* @param {import('./utils.cjs').WSSharedDoc} doc * @param {import('./utils.js').WSSharedDoc} doc
*/ */
const getContent = (objName, objType, doc) => { const getContent = (objName, objType, doc) => {
switch (objType) { switch (objType) {

View File

@ -1,11 +1,11 @@
#!/usr/bin/env node #!/usr/bin/env node
const WebSocket = require('ws') import WebSocket from 'ws'
const http = require('http') import http from 'http'
const number = require('lib0/number') import * as number from 'lib0/number'
const wss = new WebSocket.Server({ noServer: true }) import { setupWSConnection } from './utils.js'
const setupWSConnection = require('./utils.cjs').setupWSConnection
const wss = new WebSocket.Server({ noServer: true })
const host = process.env.HOST || 'localhost' const host = process.env.HOST || 'localhost'
const port = number.parseInt(process.env.PORT || '1234') const port = number.parseInt(process.env.PORT || '1234')

View File

@ -1,15 +1,14 @@
const Y = require('yjs') import * as Y from 'yjs'
const syncProtocol = require('y-protocols/sync') import * as syncProtocol from 'y-protocols/sync'
const awarenessProtocol = require('y-protocols/awareness') import * as awarenessProtocol from 'y-protocols/awareness'
const encoding = require('lib0/encoding') import * as encoding from 'lib0/encoding'
const decoding = require('lib0/decoding') import * as decoding from 'lib0/decoding'
const map = require('lib0/map') import * as map from 'lib0/map'
const debounce = require('lodash.debounce') import debounce from 'lodash.debounce'
const callbackHandler = require('./callback.cjs').callbackHandler import { callbackHandler, isCallbackSet } from './callback.js'
const isCallbackSet = require('./callback.cjs').isCallbackSet
const CALLBACK_DEBOUNCE_WAIT = parseInt(process.env.CALLBACK_DEBOUNCE_WAIT || '2000') const CALLBACK_DEBOUNCE_WAIT = parseInt(process.env.CALLBACK_DEBOUNCE_WAIT || '2000')
const CALLBACK_DEBOUNCE_MAXWAIT = parseInt(process.env.CALLBACK_DEBOUNCE_MAXWAIT || '10000') 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, * @param {{bindState: function(string,WSSharedDoc):void,
* writeState:function(string,WSSharedDoc):Promise<any>,provider:any}|null} persistence_ * writeState:function(string,WSSharedDoc):Promise<any>,provider:any}|null} persistence_
*/ */
exports.setPersistence = persistence_ => { export const setPersistence = persistence_ => {
persistence = persistence_ persistence = persistence_
} }
@ -58,14 +57,12 @@ exports.setPersistence = persistence_ => {
* @return {null|{bindState: function(string,WSSharedDoc):void, * @return {null|{bindState: function(string,WSSharedDoc):void,
* writeState:function(string,WSSharedDoc):Promise<any>}|null} used persistence layer * writeState:function(string,WSSharedDoc):Promise<any>}|null} used persistence layer
*/ */
exports.getPersistence = () => persistence export const getPersistence = () => persistence
/** /**
* @type {Map<string,WSSharedDoc>} * @type {Map<string,WSSharedDoc>}
*/ */
const docs = new Map() export const docs = new Map()
// exporting docs so that others can use it
exports.docs = docs
const messageSync = 0 const messageSync = 0
const messageAwareness = 1 const messageAwareness = 1
@ -96,11 +93,11 @@ let contentInitializor = _ydoc => Promise.resolve()
* *
* @param {(ydoc: Y.Doc) => Promise<void>} f * @param {(ydoc: Y.Doc) => Promise<void>} f
*/ */
exports.setContentInitializor = (f) => { export const setContentInitializor = (f) => {
contentInitializor = f contentInitializor = f
} }
class WSSharedDoc extends Y.Doc { export class WSSharedDoc extends Y.Doc {
/** /**
* @param {string} name * @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 * 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) * @param {boolean} gc - whether to allow gc on the doc (applies only when created)
* @return {WSSharedDoc} * @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) const doc = new WSSharedDoc(docname)
doc.gc = gc doc.gc = gc
if (persistence !== null) { if (persistence !== null) {
@ -171,8 +166,6 @@ const getYDoc = (docname, gc = true) => map.setIfUndefined(docs, docname, () =>
return doc return doc
}) })
exports.getYDoc = getYDoc
/** /**
* @param {any} conn * @param {any} conn
* @param {WSSharedDoc} doc * @param {WSSharedDoc} doc
@ -254,7 +247,7 @@ const pingTimeout = 30000
* @param {import('http').IncomingMessage} req * @param {import('http').IncomingMessage} req
* @param {any} opts * @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' conn.binaryType = 'arraybuffer'
// get doc, initialize if it does not exist yet // get doc, initialize if it does not exist yet
const doc = getYDoc(docName, gc) const doc = getYDoc(docName, gc)

View File

@ -1,18 +1,12 @@
export default { export default [{
input: './src/y-websocket.js', input: ['./src/y-websocket.js', './bin/server.js', './bin/utils.js'],
external: id => /^(lib0|yjs|y-protocols)/.test(id), external: id => /^(lib0|yjs|y-protocols|ws|lodash\.debounce|http)/.test(id),
output: [{ output: [{
name: 'y-websocket', dir: 'dist',
file: 'dist/y-websocket.cjs',
format: 'cjs', format: 'cjs',
sourcemap: true, sourcemap: true,
paths: path => { entryFileNames: '[name].cjs',
if (/^lib0\//.test(path)) { chunkFileNames: '[name]-[hash].cjs'
return `lib0/dist${path.slice(4)}.cjs`
} else if (/^y-protocols\//.test(path)) {
return `y-protocols/dist${path.slice(11)}.cjs`
}
return path
}
}] }]
} }
]