Merge pull request #29472 from overleaf/mfb-from-joi-to-zod-real-time

Mfb from joi to zod real time

GitOrigin-RevId: 657c18bae01eaeea76bf308579a7bea1388253d6
This commit is contained in:
Maria Florencia Besteiro Gonzalez
2025-11-04 16:35:38 +01:00
committed by Copybot
parent 3586b37491
commit 820ce8ad1c
7 changed files with 28 additions and 30 deletions

5
package-lock.json generated
View File

@@ -52882,6 +52882,7 @@
"@overleaf/o-error": "*",
"@overleaf/redis-wrapper": "*",
"@overleaf/settings": "*",
"@overleaf/validation-tools": "*",
"async": "^3.2.5",
"base64id": "0.1.0",
"body-parser": "^1.20.3",
@@ -52890,12 +52891,12 @@
"cookie-parser": "^1.4.6",
"express": "^4.21.2",
"express-session": "^1.17.1",
"joi": "^17.12.0",
"lodash": "^4.17.21",
"proxy-addr": "^2.0.7",
"request": "^2.88.2",
"socket.io": "github:overleaf/socket.io#0.9.19-overleaf-12",
"socket.io-client": "github:overleaf/socket.io-client#0.9.17-overleaf-5"
"socket.io-client": "github:overleaf/socket.io-client#0.9.17-overleaf-5",
"zod-validation-error": "^4.0.1"
},
"devDependencies": {
"chai": "^4.3.6",

View File

@@ -7,6 +7,7 @@ libraries/metrics/**
libraries/o-error/**
libraries/redis-wrapper/**
libraries/settings/**
libraries/validation-tools/**
package-lock.json
package.json
patches/**

View File

@@ -19,6 +19,7 @@ COPY libraries/metrics/package.json /overleaf/libraries/metrics/package.json
COPY libraries/o-error/package.json /overleaf/libraries/o-error/package.json
COPY libraries/redis-wrapper/package.json /overleaf/libraries/redis-wrapper/package.json
COPY libraries/settings/package.json /overleaf/libraries/settings/package.json
COPY libraries/validation-tools/package.json /overleaf/libraries/validation-tools/package.json
COPY services/real-time/package.json /overleaf/services/real-time/package.json
COPY patches/ /overleaf/patches/
@@ -29,6 +30,7 @@ COPY libraries/metrics/ /overleaf/libraries/metrics/
COPY libraries/o-error/ /overleaf/libraries/o-error/
COPY libraries/redis-wrapper/ /overleaf/libraries/redis-wrapper/
COPY libraries/settings/ /overleaf/libraries/settings/
COPY libraries/validation-tools/ /overleaf/libraries/validation-tools/
COPY services/real-time/ /overleaf/services/real-time/
FROM app

View File

@@ -20,6 +20,7 @@ IMAGE_CACHE ?= $(IMAGE_REPO):cache-$(shell cat \
$(MONOREPO)/libraries/o-error/package.json \
$(MONOREPO)/libraries/redis-wrapper/package.json \
$(MONOREPO)/libraries/settings/package.json \
$(MONOREPO)/libraries/validation-tools/package.json \
$(MONOREPO)/services/real-time/package.json \
$(MONOREPO)/patches/* \
| sha256sum | cut -d '-' -f1)

View File

@@ -8,17 +8,13 @@ const WebsocketAddressManager = require('./WebsocketAddressManager')
const bodyParser = require('body-parser')
const base64id = require('base64id')
const { UnexpectedArgumentsError } = require('./Errors')
const Joi = require('joi')
const { z, zz } = require('@overleaf/validation-tools')
const { isZodErrorLike } = require('zod-validation-error')
const HOSTNAME = require('node:os').hostname()
const SERVER_PING_INTERVAL = 15000
const SERVER_PING_LATENCY_THRESHOLD = 5000
const JOI_OBJECT_ID = Joi.string()
.required()
.regex(/^[0-9a-f]{24}$/)
.message('invalid id')
let Router
module.exports = Router = {
_handleError(callback, error, client, method, attrs) {
@@ -29,11 +25,11 @@ module.exports = Router = {
attrs.client_id = client.id
attrs.err = error
attrs.method = method
if (Joi.isError(error)) {
if (isZodErrorLike(error)) {
logger.info(attrs, 'validation error')
let message = 'invalid'
try {
message = error.details[0].message
message = error.issues[0].message
} catch (e) {
// ignore unexpected errors
logger.warn({ error, e }, 'unexpected validation error')
@@ -193,7 +189,7 @@ module.exports = Router = {
if (!isDebugging) {
try {
Joi.assert(projectId, JOI_OBJECT_ID)
zz.objectId().parse(projectId)
} catch (error) {
metrics.inc('socket-io.connection', 1, {
status: client.transport,
@@ -409,6 +405,11 @@ module.exports = Router = {
})
})
const joinDocSchema = z.object({
doc_id: zz.objectId(),
fromVersion: z.number().int().optional(),
options: z.object(),
})
// Variadic. The possible arguments:
// doc_id, callback
// doc_id, fromVersion, callback
@@ -442,14 +443,7 @@ module.exports = Router = {
return Router._handleInvalidArguments(client, 'joinDoc', arguments)
}
try {
Joi.assert(
{ doc_id: docId, fromVersion, options },
Joi.object({
doc_id: JOI_OBJECT_ID,
fromVersion: Joi.number().integer(),
options: Joi.object().required(),
})
)
joinDocSchema.parse({ doc_id: docId, fromVersion, options })
} catch (error) {
return Router._handleError(callback, error, client, 'joinDoc', {
disconnect: 1,
@@ -478,7 +472,7 @@ module.exports = Router = {
return Router._handleInvalidArguments(client, 'leaveDoc', arguments)
}
try {
Joi.assert(docId, JOI_OBJECT_ID)
zz.objectId().parse(docId)
} catch (error) {
return Router._handleError(callback, error, client, 'joinDoc', {
disconnect: 1,
@@ -554,6 +548,10 @@ module.exports = Router = {
}
)
const applyOtUpdateSchema = z.object({
doc_id: zz.objectId(),
update: z.object(),
})
client.on('applyOtUpdate', function (docId, update, callback) {
if (typeof callback !== 'function') {
return Router._handleInvalidArguments(
@@ -563,13 +561,7 @@ module.exports = Router = {
)
}
try {
Joi.assert(
{ doc_id: docId, update },
Joi.object({
doc_id: JOI_OBJECT_ID,
update: Joi.object().required(),
})
)
applyOtUpdateSchema.parse({ doc_id: docId, update })
} catch (error) {
return Router._handleError(callback, error, client, 'applyOtUpdate', {
disconnect: 1,

View File

@@ -22,6 +22,7 @@
"@overleaf/o-error": "*",
"@overleaf/redis-wrapper": "*",
"@overleaf/settings": "*",
"@overleaf/validation-tools": "*",
"async": "^3.2.5",
"base64id": "0.1.0",
"body-parser": "^1.20.3",
@@ -30,12 +31,12 @@
"cookie-parser": "^1.4.6",
"express": "^4.21.2",
"express-session": "^1.17.1",
"joi": "^17.12.0",
"lodash": "^4.17.21",
"proxy-addr": "^2.0.7",
"request": "^2.88.2",
"socket.io": "github:overleaf/socket.io#0.9.19-overleaf-12",
"socket.io-client": "github:overleaf/socket.io-client#0.9.17-overleaf-5"
"socket.io-client": "github:overleaf/socket.io-client#0.9.17-overleaf-5",
"zod-validation-error": "^4.0.1"
},
"devDependencies": {
"chai": "^4.3.6",

View File

@@ -328,7 +328,7 @@ describe('joinDoc', function () {
})
it('should return an invalid id error', function () {
this.error.message.should.equal('invalid id')
this.error.message.should.equal('invalid Mongo ObjectId')
})
return it('should not have joined the doc room', function (done) {