add better typings for server code

This commit is contained in:
Kevin Jahns 2024-04-28 01:24:54 +02:00
parent 12608bdaf8
commit 9e2dab6ed4
3 changed files with 24 additions and 7 deletions

View File

@ -142,11 +142,11 @@ class WSSharedDoc extends Y.Doc {
this.awareness.on('update', awarenessChangeHandler) this.awareness.on('update', awarenessChangeHandler)
this.on('update', /** @type {any} */ (updateHandler)) this.on('update', /** @type {any} */ (updateHandler))
if (isCallbackSet) { if (isCallbackSet) {
this.on('update', debounce( this.on('update', /** @type {any} */ (debounce(
callbackHandler, callbackHandler,
CALLBACK_DEBOUNCE_WAIT, CALLBACK_DEBOUNCE_WAIT,
{ maxWait: CALLBACK_DEBOUNCE_MAXWAIT } { maxWait: CALLBACK_DEBOUNCE_MAXWAIT }
)) )))
} }
this.whenInitialized = contentInitializor(this) this.whenInitialized = contentInitializor(this)
} }
@ -233,7 +233,7 @@ const closeConn = (doc, conn) => {
/** /**
* @param {WSSharedDoc} doc * @param {WSSharedDoc} doc
* @param {any} conn * @param {import('ws').WebSocket} conn
* @param {Uint8Array} m * @param {Uint8Array} m
*/ */
const send = (doc, conn, m) => { const send = (doc, conn, m) => {
@ -241,7 +241,7 @@ const send = (doc, conn, m) => {
closeConn(doc, conn) closeConn(doc, conn)
} }
try { try {
conn.send(m, /** @param {any} err */ err => { err != null && closeConn(doc, conn) }) conn.send(m, {}, err => { err != null && closeConn(doc, conn) })
} catch (e) { } catch (e) {
closeConn(doc, conn) closeConn(doc, conn)
} }
@ -250,11 +250,11 @@ const send = (doc, conn, m) => {
const pingTimeout = 30000 const pingTimeout = 30000
/** /**
* @param {any} conn * @param {import('ws').WebSocket} conn
* @param {any} 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 } = {}) => { exports.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)

16
package-lock.json generated
View File

@ -18,6 +18,7 @@
"y-websocket-server": "bin/server.cjs" "y-websocket-server": "bin/server.cjs"
}, },
"devDependencies": { "devDependencies": {
"@types/lodash.debounce": "^4.0.9",
"@types/node": "^18.15.0", "@types/node": "^18.15.0",
"@types/ws": "^8.5.10", "@types/ws": "^8.5.10",
"rollup": "^3.19.1", "rollup": "^3.19.1",
@ -41,6 +42,21 @@
"yjs": "^13.5.6" "yjs": "^13.5.6"
} }
}, },
"node_modules/@types/lodash": {
"version": "4.17.0",
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.0.tgz",
"integrity": "sha512-t7dhREVv6dbNj0q17X12j7yDG4bD/DHYX7o5/DbDxobP0HnGPgpRz2Ej77aL7TZT3DSw13fqUTj8J4mMnqa7WA==",
"dev": true
},
"node_modules/@types/lodash.debounce": {
"version": "4.0.9",
"resolved": "https://registry.npmjs.org/@types/lodash.debounce/-/lodash.debounce-4.0.9.tgz",
"integrity": "sha512-Ma5JcgTREwpLRwMM+XwBR7DaWe96nC38uCBDFKZWbNKD+osjVzdpnUSwBcqCptrp16sSOLBAUb50Car5I0TCsQ==",
"dev": true,
"dependencies": {
"@types/lodash": "*"
}
},
"node_modules/@types/node": { "node_modules/@types/node": {
"version": "18.15.0", "version": "18.15.0",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.0.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.0.tgz",

View File

@ -64,6 +64,7 @@
"y-protocols": "^1.0.5" "y-protocols": "^1.0.5"
}, },
"devDependencies": { "devDependencies": {
"@types/lodash.debounce": "^4.0.9",
"@types/node": "^18.15.0", "@types/node": "^18.15.0",
"@types/ws": "^8.5.10", "@types/ws": "^8.5.10",
"rollup": "^3.19.1", "rollup": "^3.19.1",