Debian Codegraph @

extensions/VisualEditor/lib/ve/rebaser/src/dm/ve.dm.ProtocolServer.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel protocol server class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 'use strict';
8
9 /**
10 * Protocol server
11 *
12 * Handles the abstract protocol without knowing the specific transport
13 *
14 * @param {ve.dm.DocumentStore} documentStore The persistent storage
15 * @param {Object} logger Logger class
16 * @param {Function} logger.getRelativeTimestmap Return the number of ms since the logger started
17 * @param {Function} logger.logEvent Stringify object argument to log
18 * @param {Function} logger.logServerEvent Stringify object argument to log, adding timestamp and server ID properties
19 */
20 ve.dm.ProtocolServer = function VeDmProtocolServer( documentStore, logger ) {
21 this.logger = logger;
22 this.rebaseServer = new ve.dm.RebaseServer();
23 this.lastAuthorForDoc = new Map();
24 this.loadingForDoc = new Map();
25 this.documentStore = documentStore;
26 this.logger.logServerEvent( { type: 'restart' }, 'info' );
27 };
28
29 OO.initClass( ve.dm.ProtocolServer );
30
31 ve.dm.ProtocolServer.static.palette = [
32 '1f77b4', 'ff7f0e', '2ca02c', 'd62728', '9467bd',
33 '8c564b', 'e377c2', '7f7f7f', 'bcbd22', '17becf',
34 'aec7e8', 'ffbb78', '98df8a', 'ff9896', 'c5b0d5',
35 'c49c94', 'f7b6d2', 'c7c7c7', 'dbdb8d', '9edae5'
36 ];
37
38 /**
39 * If the document is not loaded, load from storage (creating as empty if absent)
40 *
41 * @param {string} docName Name of the document
42 * @return {Promise} Resolves when loaded
43 */
44 ve.dm.ProtocolServer.prototype.ensureLoaded = function ( docName ) {
45 const rebaseServer = this.rebaseServer;
46
47 let loading = this.loadingForDoc.get( docName );
48
49 if ( loading ) {
50 return loading;
51 }
52 this.logger.logServerEvent( { type: 'ProtocolServer#load', docName: docName } );
53 loading = this.documentStore.load( docName ).then( ( change ) => {
54 this.logger.logServerEvent( {
55 type: 'ProtocolServer#loaded',
56 docName: docName,
57 length: change.getLength()
58 } );
59 rebaseServer.updateDocState( docName, null, change );
60 } );
61 this.loadingForDoc.set( docName, loading );
62 return loading;
63 };
64
65 /**
66 * Check the client's credentials, and return a connection context object
67 *
68 * If the client is not recognised and authenticated, a new client ID and token are assigned.
69 *
70 * @param {string} docName The document name
71 * @param {number|null} authorId The author ID, if any
72 * @param {number|null} token The secret token, if any
73 *
74 * @return {Object} The connection context
75 */
76 ve.dm.ProtocolServer.prototype.authenticate = function ( docName, authorId, token ) {
77 const state = this.rebaseServer.stateForDoc.get( docName ),
78 authorData = state && state.authors.get( authorId );
79
80 if ( !authorData || token !== authorData.token ) {
81 authorId = 1 + ( this.lastAuthorForDoc.get( docName ) || 0 );
82 this.lastAuthorForDoc.set( docName, authorId );
83 token = Math.random().toString( 36 ).slice( 2 );
84 }
85 const context = {
86 serverId: this.documentStore.serverId,
87 docName: docName,
88 authorId: authorId
89 };
90 this.logger.logServerEvent( {
91 type: 'newClient',
92 doc: docName,
93 authorId: context.authorId
94 } );
95 return context;
96 };
97
98 /**
99 * Add an event to the log
100 *
101 * @param {Object} context The connection context
102 * @param {Object} event Event data
103 */
104 ve.dm.ProtocolServer.prototype.onLogEvent = function ( context, event ) {
105 const ob = {};
106 ob.recvTimestamp = this.logger.getRelativeTimestamp();
107 ob.clientId = context.authorId;
108 ob.doc = context.docName;
109 for ( const key in event ) {
110 ob[ key ] = event[ key ];
111 }
112 this.logger.logEvent( ob );
113 };
114
115 /**
116 * Setup author on the server and send initialization events
117 *
118 * @param {Object} context The connection context
119 * @param {number} [startLength=0] The length of the common history
120 * @param {Function} [usernameGenerator] Function which returns a username, with an authorID argument
121 */
122 ve.dm.ProtocolServer.prototype.welcomeClient = function ( context, startLength, usernameGenerator ) {
123 const docName = context.docName,
124 serverId = context.serverId,
125 authorId = context.authorId;
126
127 if ( !startLength ) {
128 startLength = 0;
129 }
130 this.rebaseServer.updateDocState( docName, authorId, null, {
131 name: usernameGenerator ? usernameGenerator( authorId ) : 'User ' + authorId,
132 color: this.constructor.static.palette[
133 authorId % this.constructor.static.palette.length
134 ],
135 active: true
136 } );
137
138 const state = this.rebaseServer.getDocState( docName );
139 const authorData = state.authors.get( authorId );
140
141 context.sendAuthor( 'registered', {
142 serverId: serverId,
143 authorId: authorId,
144 token: authorData.token
145 } );
146 context.broadcast( 'authorChange', {
147 authorId: authorId,
148 authorData: {
149 name: authorData.name,
150 color: authorData.color
151 }
152 } );
153 // HACK Catch the client up on the current state by sending it the entire history
154 // Ideally we'd be able to initialize the client using HTML, but that's hard, see
155 // comments in the /raw handler. Keeping an updated linmod on the server could be
156 // feasible if TransactionProcessor was modified to have a "don't sync, just apply"
157 // mode and ve.dm.Document was faked with { data: …, metadata: …, store: … }
158 context.sendAuthor( 'initDoc', {
159 history: state.history.mostRecent( startLength ).serialize( true ),
160 authors: state.getActiveAuthors()
161 } );
162 };
163
164 /**
165 * Try to apply a received change, and broadcast the successful portion as rebased
166 *
167 * @param {Object} context The connection context
168 * @param {Object} data The change data
169 */
170 ve.dm.ProtocolServer.prototype.onSubmitChange = function ( context, data ) {
171 const change = ve.dm.Change.static.deserialize( data.change, true );
172 const applied = this.rebaseServer.applyChange( context.docName, context.authorId, data.backtrack, change );
173 if ( !applied.isEmpty() ) {
174 this.documentStore.onNewChange( context.docName, applied );
175 context.broadcast( 'newChange', applied.serialize( true ) );
176 }
177 };
178
179 /**
180 * Apply and broadcast an author change
181 *
182 * @param {Object} context The connection context
183 * @param {string} newData The new author data
184 */
185 ve.dm.ProtocolServer.prototype.onChangeAuthor = function ( context, newData ) {
186 this.rebaseServer.updateDocState( context.docName, context.authorId, null, {
187 name: newData.name,
188 color: newData.color
189 } );
190 context.broadcast( 'authorChange', {
191 authorId: context.authorId,
192 authorData: {
193 name: newData.name,
194 color: newData.color
195 }
196 } );
197 this.logger.logServerEvent( {
198 type: 'authorChange',
199 doc: context.docName,
200 authorId: context.authorId,
201 authorData: {
202 name: newData.name,
203 color: newData.color
204 }
205 } );
206 };
207
208 /**
209 * Apply and broadcast a disconnection (which may be temporary)
210 *
211 * @param {Object} context The connection context
212 */
213 ve.dm.ProtocolServer.prototype.onDisconnect = function ( context ) {
214 this.rebaseServer.updateDocState( context.docName, context.authorId, null, {
215 active: false,
216 continueBase: null,
217 rejections: null
218 } );
219 context.broadcast( 'authorDisconnect', context.authorId );
220 this.logger.logServerEvent( {
221 type: 'disconnect',
222 doc: context.docName,
223 authorId: context.authorId
224 } );
225 };