Skip to content

Commit

Permalink
make this a proper esm module
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Mar 20, 2024
1 parent 89fb1e3 commit c3d14cf
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 37 deletions.
9 changes: 5 additions & 4 deletions bin/callback.js → bin/callback.cjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
const http = require('http')
const number = require('lib0/number')

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

/**
* @param {Uint8Array} update
* @param {any} origin
* @param {WSSharedDoc} doc
* @param {import('./utils.cjs').WSSharedDoc} doc
*/
exports.callbackHandler = (update, origin, doc) => {
const room = doc.name
Expand All @@ -25,7 +26,7 @@ exports.callbackHandler = (update, origin, doc) => {
content: getContent(sharedObjectName, sharedObjectType, doc).toJSON()
}
})
callbackRequest(CALLBACK_URL, CALLBACK_TIMEOUT, dataToSend)
CALLBACK_URL && callbackRequest(CALLBACK_URL, CALLBACK_TIMEOUT, dataToSend)
}

/**
Expand Down Expand Up @@ -62,7 +63,7 @@ const callbackRequest = (url, timeout, data) => {
/**
* @param {string} objName
* @param {string} objType
* @param {WSSharedDoc} doc
* @param {import('./utils.cjs').WSSharedDoc} doc
*/
const getContent = (objName, objType, doc) => {
switch (objType) {
Expand Down
7 changes: 4 additions & 3 deletions bin/server.js → bin/server.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
*/
const WebSocket = require('ws')
const http = require('http')
const number = require('lib0/number')
const wss = new WebSocket.Server({ noServer: true })
const setupWSConnection = require('./utils.js').setupWSConnection
const setupWSConnection = require('./utils.cjs').setupWSConnection

const host = process.env.HOST || 'localhost'
const port = process.env.PORT || 1234
const port = number.parseInt(process.env.PORT || '1234')

const server = http.createServer((request, response) => {
const server = http.createServer((_request, response) => {
response.writeHead(200, { 'Content-Type': 'text/plain' })
response.end('okay')
})
Expand Down
28 changes: 16 additions & 12 deletions bin/utils.js → bin/utils.cjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const Y = require('yjs')
const syncProtocol = require('y-protocols/dist/sync.cjs')
const awarenessProtocol = require('y-protocols/dist/awareness.cjs')
const syncProtocol = require('y-protocols/sync')
const awarenessProtocol = require('y-protocols/awareness')

const encoding = require('lib0/dist/encoding.cjs')
const decoding = require('lib0/dist/decoding.cjs')
const map = require('lib0/dist/map.cjs')
const encoding = require('lib0/encoding')
const decoding = require('lib0/decoding')
const map = require('lib0/map')

const debounce = require('lodash.debounce')

const callbackHandler = require('./callback.js').callbackHandler
const isCallbackSet = require('./callback.js').isCallbackSet
const callbackHandler = require('./callback.cjs').callbackHandler
const isCallbackSet = require('./callback.cjs').isCallbackSet

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_WAIT = parseInt(process.env.CALLBACK_DEBOUNCE_WAIT || '2000')
const CALLBACK_DEBOUNCE_MAXWAIT = parseInt(process.env.CALLBACK_DEBOUNCE_MAXWAIT || '10000')

const wsReadyStateConnecting = 0
const wsReadyStateOpen = 1
Expand Down Expand Up @@ -73,10 +73,11 @@ const messageAwareness = 1

/**
* @param {Uint8Array} update
* @param {any} origin
* @param {any} _origin
* @param {WSSharedDoc} doc
* @param {any} _tr
*/
const updateHandler = (update, origin, doc) => {
const updateHandler = (update, _origin, doc, _tr) => {
const encoder = encoding.createEncoder()
encoding.writeVarUint(encoder, messageSync)
syncProtocol.writeUpdate(encoder, update)
Expand Down Expand Up @@ -124,7 +125,7 @@ class WSSharedDoc extends Y.Doc {
})
}
this.awareness.on('update', awarenessChangeHandler)
this.on('update', updateHandler)
this.on('update', /** @type {any} */ (updateHandler))
if (isCallbackSet) {
this.on('update', debounce(
callbackHandler,
Expand All @@ -135,6 +136,8 @@ class WSSharedDoc extends Y.Doc {
}
}

exports.WSSharedDoc = WSSharedDoc

/**
* Gets a Y.Doc by name, whether in memory or on disk
*
Expand Down Expand Up @@ -183,6 +186,7 @@ const messageListener = (conn, doc, message) => {
}
} catch (err) {
console.error(err)
// @ts-ignore
doc.emit('error', [err])
}
}
Expand Down
32 changes: 21 additions & 11 deletions package-lock.json

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

14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@
"main": "./dist/y-websocket.cjs",
"module": "./src/y-websocket.js",
"types": "./dist/src/y-websocket.d.ts",
"type": "module",
"sideEffects": false,
"funding": {
"type": "GitHub Sponsors ❤",
"url": "https://github.com/sponsors/dmonad"
},
"scripts": {
"start": "node ./bin/server.js",
"start": "node ./bin/server.cjs",
"dist": "rm -rf dist && rollup -c && tsc",
"lint": "standard && tsc",
"test": "npm run lint",
"preversion": "npm run lint && npm run dist && test -e dist/src/y-websocket.d.ts && test -e dist/y-websocket.cjs"
},
"bin": {
"y-websocket-server": "./bin/server.js",
"y-websocket": "./bin/server.js"
"y-websocket-server": "./bin/server.cjs",
"y-websocket": "./bin/server.cjs"
},
"files": [
"dist/*",
Expand All @@ -28,13 +29,14 @@
],
"exports": {
"./package.json": "./package.json",
"./bin/utils": "./bin/utils.js",
"./bin/callback": "./bin/callback.js",
"./bin/utils": "./bin/utils.cjs",
"./bin/callback": "./bin/callback.cjs",
".": {
"module": "./src/y-websocket.js",
"import": "./src/y-websocket.js",
"require": "./dist/y-websocket.cjs",
"types": "./dist/src/y-websocket.d.ts"
"types": "./dist/src/y-websocket.d.ts",
"default": "./dist/y-websocket.js"
}
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
// "maxNodeModuleJsDepth": 5
},
"include": ["./src/y-websocket.js"]
"include": ["./src/y-websocket.js", "./bin/**/*"]
}

0 comments on commit c3d14cf

Please sign in to comment.