topazcube 0.1.18 → 0.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/server.ts CHANGED
@@ -19,7 +19,7 @@ import fastjsonpatch from 'fast-json-patch'
19
19
  import { WebSocketServer, WebSocket } from 'ws'
20
20
  import { MongoClient, Db } from 'mongodb'
21
21
  import { glMatrix, vec3, quat } from 'gl-matrix'
22
- import wrtc from '@roamhq/wrtc' // Server-side WebRTC implementation
22
+ // WebRTC implementation loaded conditionally when needed
23
23
 
24
24
  glMatrix.setMatrixArrayType(Array)
25
25
 
@@ -97,10 +97,12 @@ export default class TopazCubeServer {
97
97
  documents: Record<string, any> = {}
98
98
  isLoading: Record<string, boolean> = {}
99
99
  _documentChanges: Record<string, any[]> = {}
100
+ _documentChanged: Record<string, boolean> = {}
100
101
  _documentState: Record<string, any> = {}
101
102
 
102
103
  update = 0
103
104
  lastUpdate = 0
105
+ _saveiv: any = null
104
106
  _loopiv: any = null
105
107
  _statsiv: any = null
106
108
  _stillUpdating = false
@@ -286,6 +288,7 @@ export default class TopazCubeServer {
286
288
  )
287
289
  if (!this._documentChanges[name]) {
288
290
  this._documentChanges[name] = []
291
+ this._documentChanged[name] = false
289
292
  }
290
293
  }
291
294
 
@@ -350,6 +353,9 @@ export default class TopazCubeServer {
350
353
  this._statsiv = setInterval(() => {
351
354
  this._doStats()
352
355
  }, 1000)
356
+ this._saveiv = setInterval(() => {
357
+ this._saveChanges()
358
+ }, 60000)
353
359
  }
354
360
 
355
361
  _loop(): void {
@@ -463,12 +469,14 @@ export default class TopazCubeServer {
463
469
  let name = message.n
464
470
  if (!this._documentChanges[name]) {
465
471
  this._documentChanges[name] = []
472
+ this._documentChanged[name] = false
466
473
  }
467
474
  for (let op of message.p) {
468
475
  if (!this.canSync(client, name, op)) {
469
476
  continue
470
477
  }
471
478
  this._documentChanges[name].push(op)
479
+ this._documentChanged[name] = true
472
480
  let dop = msgop(op)
473
481
  applyOperation(this.documents[name], dop)
474
482
  }
@@ -485,12 +493,10 @@ export default class TopazCubeServer {
485
493
  client.ctdiff = message.ct + ping / 2 - time
486
494
  client.ping = ping
487
495
  //this.log(time, "PENG ping, ctdiff", message, ping, client.ctdiff, "ms")
488
- /*
489
496
  } else if (message.c == 'rtc-offer') {
490
497
  this._processOffer(client, message)
491
498
  } else if (message.c == 'rtc-candidate') {
492
499
  this._processICECandidate(client, message)
493
- */
494
500
  } else if (message.c == 'sub') {
495
501
  await this._checkDocument(message.n, client)
496
502
  if (!this.documents[message.n]) {
@@ -790,6 +796,7 @@ export default class TopazCubeServer {
790
796
 
791
797
  _onDocumentChange(name: string, op: any, target: any, path: any, value: any): void {
792
798
  this._documentChanges[name]?.push(opmsg(op, target, path, value))
799
+ this._documentChanged[name] = true
793
800
  }
794
801
 
795
802
  propertyChange(name: string, id: string | number, property: string): void {
@@ -806,8 +813,27 @@ export default class TopazCubeServer {
806
813
 
807
814
  /*= WEBRTC ===================================================================*/
808
815
 
816
+ private _wrtc: any = null
817
+
818
+ private async _loadWebRTC(): Promise<any> {
819
+ if (!this._wrtc) {
820
+ try {
821
+ this._wrtc = await import('@roamhq/wrtc')
822
+ } catch (error) {
823
+ this.error('WebRTC module not available:', error)
824
+ throw new Error('WebRTC functionality requires @roamhq/wrtc and platform-specific binary packages')
825
+ }
826
+ }
827
+ return this._wrtc
828
+ }
809
829
 
810
830
  async _processOffer(client: ClientType, data: any): Promise<void> {
831
+ if (!this.allowWebRTC) {
832
+ this.warn('WebRTC is disabled')
833
+ return
834
+ }
835
+
836
+ const wrtc = await this._loadWebRTC()
811
837
  //this.log("RTC: Offer received", data);
812
838
  const peerConnection = new (wrtc as any).RTCPeerConnection({
813
839
  iceServers: [
@@ -1137,6 +1163,18 @@ export default class TopazCubeServer {
1137
1163
  }
1138
1164
  }
1139
1165
 
1166
+ async _saveChanges(): Promise<void> {
1167
+ if (!this.allowSave) {
1168
+ return
1169
+ }
1170
+ for (let name in this._documentChanged) {
1171
+ if (this._documentChanged[name]) {
1172
+ await this._saveDocument(name)
1173
+ this._documentChanged[name] = false
1174
+ }
1175
+ }
1176
+ }
1177
+
1140
1178
  _initServerDocument(): void {
1141
1179
  this.documents['_server'] = {
1142
1180
  nextUID: 100,
@@ -1150,6 +1188,8 @@ export default class TopazCubeServer {
1150
1188
  this.log('\nEXIT: Caught interrupt signal ' + signal)
1151
1189
  this._exited = true
1152
1190
  clearInterval(this._loopiv)
1191
+ clearInterval(this._statsiv)
1192
+ clearInterval(this._saveiv)
1153
1193
  this.onBeforeExit()
1154
1194
  this.broadcast({ server: 'Going down' })
1155
1195
  this._saveAllDocuments()