(options: SyncOptions)
| 477 | } |
| 478 | |
| 479 | private async createConnection(options: SyncOptions) { |
| 480 | if (this.connection) return; |
| 481 | |
| 482 | const { HubConnectionBuilder, HttpTransportType, JsonHubProtocol } = |
| 483 | await import("@microsoft/signalr"); |
| 484 | |
| 485 | const tokenManager = new TokenManager(this.db.kv, this.db.eventManager); |
| 486 | this.connection = new HubConnectionBuilder() |
| 487 | .withUrl(`${Constants.API_HOST}/hubs/sync/v2`, { |
| 488 | accessTokenFactory: async () => { |
| 489 | const token = await tokenManager.getAccessToken(); |
| 490 | if (!token) throw new Error("Failed to get access token."); |
| 491 | return token; |
| 492 | }, |
| 493 | skipNegotiation: true, |
| 494 | transport: HttpTransportType.WebSockets, |
| 495 | logger: { |
| 496 | log: (level, message) => { |
| 497 | const scopedLogger = logger.scope("SignalR::SyncHub"); |
| 498 | switch (level) { |
| 499 | case LogLevel.Critical: |
| 500 | return scopedLogger.fatal(new Error(message)); |
| 501 | case LogLevel.Error: { |
| 502 | this.db.eventManager.publish(EVENTS.syncAborted, message); |
| 503 | return scopedLogger.error(new Error(message)); |
| 504 | } |
| 505 | case LogLevel.Warning: |
| 506 | return scopedLogger.warn(message); |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | }) |
| 511 | .withHubProtocol(new JsonHubProtocol()) |
| 512 | .build(); |
| 513 | this.connection.serverTimeoutInMilliseconds = 60 * 1000 * 5; |
| 514 | this.connection.on("PushCompletedV2", (deviceId: string) => |
| 515 | this.onPushCompleted(deviceId) |
| 516 | ); |
| 517 | this.connection.on("SendVaultKey", async (vaultKey) => { |
| 518 | if (this.connection?.state !== HubConnectionState.Connected) return false; |
| 519 | |
| 520 | if ( |
| 521 | vaultKey && |
| 522 | vaultKey.cipher !== null && |
| 523 | vaultKey.iv !== null && |
| 524 | vaultKey.salt !== null && |
| 525 | vaultKey.length > 0 |
| 526 | ) { |
| 527 | const vault = await this.db.vaults.default(); |
| 528 | if (!vault) |
| 529 | await migrateVaultKey( |
| 530 | this.db, |
| 531 | vaultKey, |
| 532 | 5.9, |
| 533 | CURRENT_DATABASE_VERSION |
| 534 | ); |
| 535 | } |
| 536 |
no test coverage detected