MCPcopy
hub / github.com/tldraw/tldraw / handleConnectRequest

Method handleConnectRequest

packages/sync-core/src/lib/TLSyncRoom.ts:863–988  ·  view source on GitHub ↗
(
		session: RoomSession<R, SessionMeta>,
		message: Extract<TLSocketClientSentEvent<R>, { type: 'connect' }>
	)

Source from the content-addressed store, hash-verified

861 }
862
863 private handleConnectRequest(
864 session: RoomSession<R, SessionMeta>,
865 message: Extract<TLSocketClientSentEvent<R>, { type: 'connect' }>
866 ) {
867 // if the protocol versions don't match, disconnect the client
868 // we will eventually want to try to make our protocol backwards compatible to some degree
869 // and have a MIN_PROTOCOL_VERSION constant that the TLSyncRoom implements support for
870 let theirProtocolVersion = message.protocolVersion
871 // 5 is the same as 6
872 if (theirProtocolVersion === 5) {
873 theirProtocolVersion = 6
874 }
875 // 6 is almost the same as 7
876 session.requiresLegacyRejection = theirProtocolVersion === 6
877 if (theirProtocolVersion === 6) {
878 theirProtocolVersion++
879 }
880 if (theirProtocolVersion === 7) {
881 theirProtocolVersion++
882 session.supportsStringAppend = false
883 }
884
885 if (theirProtocolVersion == null || theirProtocolVersion < getTlsyncProtocolVersion()) {
886 this.rejectSession(session.sessionId, TLSyncErrorCloseEventReason.CLIENT_TOO_OLD)
887 return
888 } else if (theirProtocolVersion > getTlsyncProtocolVersion()) {
889 this.rejectSession(session.sessionId, TLSyncErrorCloseEventReason.SERVER_TOO_OLD)
890 return
891 }
892 // If the client's store is at a different version to ours, it could cause corruption.
893 // We should disconnect the client and ask them to refresh.
894 if (message.schema == null) {
895 this.rejectSession(session.sessionId, TLSyncErrorCloseEventReason.CLIENT_TOO_OLD)
896 return
897 }
898 const migrations = this.schema.getMigrationsSince(message.schema)
899 if (!migrations.ok) {
900 this.rejectSession(session.sessionId, this.getVersionMismatchReason(message.schema))
901 return
902 }
903 // The client's schema is older than ours, but we can't migrate our data down to their
904 // version (a migration isn't record-scoped or has no down migration), so they're too old.
905 if (migrations.value.some((m) => m.scope !== 'record' || !m.down)) {
906 this.rejectSession(session.sessionId, TLSyncErrorCloseEventReason.CLIENT_TOO_OLD)
907 return
908 }
909
910 const sessionSchema = isEqual(message.schema, this.serializedSchema)
911 ? this.serializedSchema
912 : message.schema
913
914 const requiresDownMigrations = migrations.value.length > 0
915
916 const connect = async (msg: Extract<TLSocketServerSentEvent<R>, { type: 'connect' }>) => {
917 this.sessions.set(session.sessionId, {
918 state: RoomSessionState.Connected,
919 sessionId: session.sessionId,
920 presenceId: session.presenceId,

Callers 1

handleMessageMethod · 0.95

Calls 15

rejectSessionMethod · 0.95
broadcastChangesMethod · 0.95
getTlsyncProtocolVersionFunction · 0.90
toNetworkDiffFunction · 0.90
isEqualFunction · 0.85
connectFunction · 0.85
getMigrationsSinceMethod · 0.80
filterMethod · 0.80
transactionMethod · 0.65
getChangesSinceMethod · 0.65

Tested by

no test coverage detected