MCPcopy
hub / github.com/streetwriters/notesnook / createConnection

Method createConnection

packages/core/src/api/sync/index.ts:479–592  ·  view source on GitHub ↗
(options: SyncOptions)

Source from the content-addressed store, hash-verified

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

Callers 1

startMethod · 0.95

Calls 15

getAccessTokenMethod · 0.95
onPushCompletedMethod · 0.95
processChunkMethod · 0.95
migrateVaultKeyFunction · 0.85
sendSyncProgressEventFunction · 0.85
handleInboxItemsFunction · 0.85
buildMethod · 0.80
fatalMethod · 0.80
warnMethod · 0.80
defaultMethod · 0.80
getDataEncryptionKeysMethod · 0.80
infoMethod · 0.80

Tested by

no test coverage detected