From e533a0d0c5a5c4ce6911ae45262bcc8fd2a29019 Mon Sep 17 00:00:00 2001 From: Kallyn Gowdy Date: Fri, 1 Nov 2024 13:47:56 -0400 Subject: [PATCH] feat: Cleanup some things --- .../documents/RemoteYjsSharedDocument.ts | 116 ++++++++---------- src/aux-runtime/runtime/AuxLibrary.ts | 33 +++-- src/aux-vm/vm/BaseAuxChannel.ts | 7 ++ 3 files changed, 77 insertions(+), 79 deletions(-) diff --git a/src/aux-common/documents/RemoteYjsSharedDocument.ts b/src/aux-common/documents/RemoteYjsSharedDocument.ts index 4ba756a66..54a2f56d7 100644 --- a/src/aux-common/documents/RemoteYjsSharedDocument.ts +++ b/src/aux-common/documents/RemoteYjsSharedDocument.ts @@ -43,6 +43,7 @@ import { InstRecordsClient, MaxInstSizeReachedClientError, RateLimitExceededMessage, + WebsocketErrorInfo, } from '../websockets'; import { SharedDocumentConfig } from './SharedDocumentConfig'; import { PartitionAuthSource } from '../partitions/PartitionAuthSource'; @@ -59,6 +60,7 @@ import { APPLY_UPDATES_TO_INST_TRANSACTION_ORIGIN, } from './YjsSharedDocument'; import { SharedDocumentServices } from './SharedDocumentFactories'; +import { KnownErrorCodes } from '../rpc/ErrorCodes'; export function createRemoteClientYjsSharedDocument( config: SharedDocumentConfig, @@ -116,8 +118,10 @@ export class RemoteYjsSharedDocument connect(): void { if (!this._temporary && this._persistence?.saveToIndexedDb) { - console.log('[RemoteYjsPartition] Using IndexedDB persistence'); - const name = `docs/${this._recordName ?? ''}/${this._inst}/${ + console.log( + '[RemoteYjsSharedDocument] Using IndexedDB persistence' + ); + const name = `${this._recordName ?? ''}/${this._inst}/${ this._branch }`; this._indexeddb = new YjsIndexedDBPersistence(name, this._doc); @@ -256,41 +260,9 @@ export class RemoteYjsSharedDocument } else if (event.type === 'repo/watch_branch_result') { if (event.success === false) { const errorCode = event.errorCode; - if ( - errorCode === 'not_authorized' || - errorCode === - 'subscription_limit_reached' || - errorCode === 'inst_not_found' || - errorCode === 'record_not_found' || - errorCode === 'invalid_record_key' || - errorCode === 'invalid_token' || - errorCode === - 'unacceptable_connection_id' || - errorCode === - 'unacceptable_connection_token' || - errorCode === 'user_is_banned' || - errorCode === 'not_logged_in' || - errorCode === 'session_expired' - ) { + if (this._isNotAuthorizedErrorCode(errorCode)) { const { type, ...error } = event; - this._onStatusUpdated.next({ - type: 'authorization', - authorized: false, - error: error, - }); - this._authSource.sendAuthRequest({ - type: 'request', - kind: 'not_authorized', - errorCode: event.errorCode, - errorMessage: event.errorMessage, - origin: this._client.connection.origin, - reason: event.reason, - resource: { - type: 'inst', - recordName: this._recordName, - inst: this._inst, - }, - }); + this._handleNotAuthorized(error); } } } @@ -345,42 +317,50 @@ export class RemoteYjsSharedDocument if (event.kind === 'max_size_reached') { this._onMaxSizeReached(event); } else if (event.kind === 'error') { - const errorCode = event.info.errorCode; - if ( - errorCode === 'not_authorized' || - errorCode === 'subscription_limit_reached' || - errorCode === 'inst_not_found' || - errorCode === 'record_not_found' || - errorCode === 'invalid_record_key' || - errorCode === 'invalid_token' || - errorCode === 'unacceptable_connection_id' || - errorCode === 'unacceptable_connection_token' || - errorCode === 'user_is_banned' || - errorCode === 'not_logged_in' || - errorCode === 'session_expired' - ) { - this._onStatusUpdated.next({ - type: 'authorization', - authorized: false, - error: event.info, - }); - this._authSource.sendAuthRequest({ - type: 'request', - kind: 'not_authorized', - errorCode: event.info.errorCode, - errorMessage: event.info.errorMessage, - origin: this._client.connection.origin, - reason: event.info.reason, - resource: { - type: 'inst', - recordName: this._recordName, - inst: this._inst, - }, - }); + const error = event.info; + if (this._isNotAuthorizedErrorCode(error.errorCode)) { + this._handleNotAuthorized(error); } } } + private _isNotAuthorizedErrorCode(errorCode: KnownErrorCodes): boolean { + return ( + errorCode === 'not_authorized' || + errorCode === 'subscription_limit_reached' || + errorCode === 'inst_not_found' || + errorCode === 'record_not_found' || + errorCode === 'invalid_record_key' || + errorCode === 'invalid_token' || + errorCode === 'unacceptable_connection_id' || + errorCode === 'unacceptable_connection_token' || + errorCode === 'user_is_banned' || + errorCode === 'not_logged_in' || + errorCode === 'session_expired' + ); + } + + private _handleNotAuthorized(error: WebsocketErrorInfo) { + this._onStatusUpdated.next({ + type: 'authorization', + authorized: false, + error: error, + }); + this._authSource.sendAuthRequest({ + type: 'request', + kind: 'not_authorized', + errorCode: error.errorCode, + errorMessage: error.errorMessage, + origin: this._client.connection.origin, + reason: error.reason, + resource: { + type: 'inst', + recordName: this._recordName, + inst: this._inst, + }, + }); + } + /** * Handles a client event that was received from the server. * @param event The event that was received. diff --git a/src/aux-runtime/runtime/AuxLibrary.ts b/src/aux-runtime/runtime/AuxLibrary.ts index 218d6eb41..03a8c4c7f 100644 --- a/src/aux-runtime/runtime/AuxLibrary.ts +++ b/src/aux-runtime/runtime/AuxLibrary.ts @@ -11169,21 +11169,27 @@ export function createDefaultLibrary(context: AuxGlobalContext) { name?: string ): Promise { const task = context.createTask(); + let recordName: string; + let instName: string; + let branchName: string; - let event: LoadSharedDocumentAction; if (!inst && !name) { - inst = getCurrentServer(); - const recordName = getCurrentInstRecord(); - event = loadSharedDocument( - recordName, - inst, - recordOrName, - task.taskId - ); + instName = getCurrentServer(); + recordName = getCurrentInstRecord(); + branchName = recordOrName; } else { - event = loadSharedDocument(recordOrName, inst, name, task.taskId); + recordName = recordOrName; + instName = inst; + branchName = name; } + const event = loadSharedDocument( + recordName, + instName, + `doc/${branchName}`, + task.taskId + ); + return addAsyncAction(task, event); } @@ -11193,7 +11199,12 @@ export function createDefaultLibrary(context: AuxGlobalContext) { */ function getLocalDocument(name: string): Promise { const task = context.createTask(); - const event = loadSharedDocument(null, null, name, task.taskId); + const event = loadSharedDocument( + null, + null, + `doc/${name}`, + task.taskId + ); return addAsyncAction(task, event); } diff --git a/src/aux-vm/vm/BaseAuxChannel.ts b/src/aux-vm/vm/BaseAuxChannel.ts index 9c190b5a6..954ce6f57 100644 --- a/src/aux-vm/vm/BaseAuxChannel.ts +++ b/src/aux-vm/vm/BaseAuxChannel.ts @@ -1017,6 +1017,13 @@ export abstract class BaseAuxChannel implements AuxChannel, SubscriptionLike { connectionProtocol: this._config.config.causalRepoConnectionProtocol, }; + + if (!hasValue(event.inst) && hasValue(event.branch)) { + config.localPersistence = { + saveToIndexedDb: true, + }; + } + let doc = await this._createSharedDocument(config, this._services); if (!doc) { return;