Skip to content

Commit 6a29fd8

Browse files
committed
transform server to a proper esm module
1 parent 62a9aa1 commit 6a29fd8

File tree

4 files changed

+38
-51
lines changed

4 files changed

+38
-51
lines changed

bin/callback.cjs renamed to bin/callback.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
const http = require('http')
2-
const number = require('lib0/number')
1+
import http from 'http'
2+
import * as number from 'lib0/number'
33

44
const CALLBACK_URL = process.env.CALLBACK_URL ? new URL(process.env.CALLBACK_URL) : null
55
const CALLBACK_TIMEOUT = number.parseInt(process.env.CALLBACK_TIMEOUT || '5000')
66
const CALLBACK_OBJECTS = process.env.CALLBACK_OBJECTS ? JSON.parse(process.env.CALLBACK_OBJECTS) : {}
77

8-
exports.isCallbackSet = !!CALLBACK_URL
8+
export const isCallbackSet = !!CALLBACK_URL
99

1010
/**
1111
* @param {Uint8Array} update
1212
* @param {any} origin
13-
* @param {import('./utils.cjs').WSSharedDoc} doc
13+
* @param {import('./utils.js').WSSharedDoc} doc
1414
*/
15-
exports.callbackHandler = (update, origin, doc) => {
15+
export const callbackHandler = (update, origin, doc) => {
1616
const room = doc.name
1717
const dataToSend = {
1818
room,
@@ -63,7 +63,7 @@ const callbackRequest = (url, timeout, data) => {
6363
/**
6464
* @param {string} objName
6565
* @param {string} objType
66-
* @param {import('./utils.cjs').WSSharedDoc} doc
66+
* @param {import('./utils.js').WSSharedDoc} doc
6767
*/
6868
const getContent = (objName, objType, doc) => {
6969
switch (objType) {

bin/server.cjs renamed to bin/server.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/usr/bin/env node
22

3-
const WebSocket = require('ws')
4-
const http = require('http')
5-
const number = require('lib0/number')
6-
const wss = new WebSocket.Server({ noServer: true })
7-
const setupWSConnection = require('./utils.cjs').setupWSConnection
3+
import WebSocket from 'ws'
4+
import http from 'http'
5+
import * as number from 'lib0/number'
6+
import { setupWSConnection } from './utils.js'
87

8+
const wss = new WebSocket.Server({ noServer: true })
99
const host = process.env.HOST || 'localhost'
1010
const port = number.parseInt(process.env.PORT || '1234')
1111

bin/utils.cjs renamed to bin/utils.js

+15-22
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
const Y = require('yjs')
2-
const syncProtocol = require('y-protocols/sync')
3-
const awarenessProtocol = require('y-protocols/awareness')
1+
import * as Y from 'yjs'
2+
import * as syncProtocol from 'y-protocols/sync'
3+
import * as awarenessProtocol from 'y-protocols/awareness'
44

5-
const encoding = require('lib0/encoding')
6-
const decoding = require('lib0/decoding')
7-
const map = require('lib0/map')
5+
import * as encoding from 'lib0/encoding'
6+
import * as decoding from 'lib0/decoding'
7+
import * as map from 'lib0/map'
88

9-
const debounce = require('lodash.debounce')
9+
import debounce from 'lodash.debounce'
1010

11-
const callbackHandler = require('./callback.cjs').callbackHandler
12-
const isCallbackSet = require('./callback.cjs').isCallbackSet
11+
import { callbackHandler, isCallbackSet } from './callback.js'
1312

1413
const CALLBACK_DEBOUNCE_WAIT = parseInt(process.env.CALLBACK_DEBOUNCE_WAIT || '2000')
1514
const CALLBACK_DEBOUNCE_MAXWAIT = parseInt(process.env.CALLBACK_DEBOUNCE_MAXWAIT || '10000')
@@ -50,22 +49,20 @@ if (typeof persistenceDir === 'string') {
5049
* @param {{bindState: function(string,WSSharedDoc):void,
5150
* writeState:function(string,WSSharedDoc):Promise<any>,provider:any}|null} persistence_
5251
*/
53-
exports.setPersistence = persistence_ => {
52+
export const setPersistence = persistence_ => {
5453
persistence = persistence_
5554
}
5655

5756
/**
5857
* @return {null|{bindState: function(string,WSSharedDoc):void,
5958
* writeState:function(string,WSSharedDoc):Promise<any>}|null} used persistence layer
6059
*/
61-
exports.getPersistence = () => persistence
60+
export const getPersistence = () => persistence
6261

6362
/**
6463
* @type {Map<string,WSSharedDoc>}
6564
*/
66-
const docs = new Map()
67-
// exporting docs so that others can use it
68-
exports.docs = docs
65+
export const docs = new Map()
6966

7067
const messageSync = 0
7168
const messageAwareness = 1
@@ -96,11 +93,11 @@ let contentInitializor = _ydoc => Promise.resolve()
9693
*
9794
* @param {(ydoc: Y.Doc) => Promise<void>} f
9895
*/
99-
exports.setContentInitializor = (f) => {
96+
export const setContentInitializor = (f) => {
10097
contentInitializor = f
10198
}
10299

103-
class WSSharedDoc extends Y.Doc {
100+
export class WSSharedDoc extends Y.Doc {
104101
/**
105102
* @param {string} name
106103
*/
@@ -152,16 +149,14 @@ class WSSharedDoc extends Y.Doc {
152149
}
153150
}
154151

155-
exports.WSSharedDoc = WSSharedDoc
156-
157152
/**
158153
* Gets a Y.Doc by name, whether in memory or on disk
159154
*
160155
* @param {string} docname - the name of the Y.Doc to find or create
161156
* @param {boolean} gc - whether to allow gc on the doc (applies only when created)
162157
* @return {WSSharedDoc}
163158
*/
164-
const getYDoc = (docname, gc = true) => map.setIfUndefined(docs, docname, () => {
159+
export const getYDoc = (docname, gc = true) => map.setIfUndefined(docs, docname, () => {
165160
const doc = new WSSharedDoc(docname)
166161
doc.gc = gc
167162
if (persistence !== null) {
@@ -171,8 +166,6 @@ const getYDoc = (docname, gc = true) => map.setIfUndefined(docs, docname, () =>
171166
return doc
172167
})
173168

174-
exports.getYDoc = getYDoc
175-
176169
/**
177170
* @param {any} conn
178171
* @param {WSSharedDoc} doc
@@ -254,7 +247,7 @@ const pingTimeout = 30000
254247
* @param {import('http').IncomingMessage} req
255248
* @param {any} opts
256249
*/
257-
exports.setupWSConnection = (conn, req, { docName = (req.url || '').slice(1).split('?')[0], gc = true } = {}) => {
250+
export const setupWSConnection = (conn, req, { docName = (req.url || '').slice(1).split('?')[0], gc = true } = {}) => {
258251
conn.binaryType = 'arraybuffer'
259252
// get doc, initialize if it does not exist yet
260253
const doc = getYDoc(docName, gc)

rollup.config.mjs

+12-18
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
export default {
2-
input: './src/y-websocket.js',
3-
external: id => /^(lib0|yjs|y-protocols)/.test(id),
4-
output: [{
5-
name: 'y-websocket',
6-
file: 'dist/y-websocket.cjs',
7-
format: 'cjs',
8-
sourcemap: true,
9-
paths: path => {
10-
if (/^lib0\//.test(path)) {
11-
return `lib0/dist${path.slice(4)}.cjs`
12-
} else if (/^y-protocols\//.test(path)) {
13-
return `y-protocols/dist${path.slice(11)}.cjs`
14-
}
15-
return path
16-
}
17-
}]
18-
}
1+
export default [{
2+
input: ['./src/y-websocket.js', './bin/server.js', './bin/utils.js'],
3+
external: id => /^(lib0|yjs|y-protocols|ws|lodash\.debounce|http)/.test(id),
4+
output: [{
5+
dir: 'dist',
6+
format: 'cjs',
7+
sourcemap: true,
8+
entryFileNames: '[name].cjs',
9+
chunkFileNames: '[name]-[hash].cjs'
10+
}]
11+
}
12+
]

0 commit comments

Comments
 (0)