Skip to content

Commit

Permalink
feat: Cleanup some things
Browse files Browse the repository at this point in the history
  • Loading branch information
KallynGowdy committed Nov 1, 2024
1 parent 4839f0a commit e533a0d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 79 deletions.
116 changes: 48 additions & 68 deletions src/aux-common/documents/RemoteYjsSharedDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
InstRecordsClient,
MaxInstSizeReachedClientError,
RateLimitExceededMessage,
WebsocketErrorInfo,
} from '../websockets';
import { SharedDocumentConfig } from './SharedDocumentConfig';
import { PartitionAuthSource } from '../partitions/PartitionAuthSource';
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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.
Expand Down
33 changes: 22 additions & 11 deletions src/aux-runtime/runtime/AuxLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11169,21 +11169,27 @@ export function createDefaultLibrary(context: AuxGlobalContext) {
name?: string
): Promise<SharedDocument> {
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);
}

Expand All @@ -11193,7 +11199,12 @@ export function createDefaultLibrary(context: AuxGlobalContext) {
*/
function getLocalDocument(name: string): Promise<SharedDocument> {
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);
}

Expand Down
7 changes: 7 additions & 0 deletions src/aux-vm/vm/BaseAuxChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e533a0d

Please sign in to comment.