mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-19 15:26:59 +08:00
feat: data center change
This commit is contained in:
@@ -1,207 +1,198 @@
|
||||
import { uuidv4 } from '@blocksuite/store';
|
||||
import { getDataCenter } from './../index';
|
||||
import { DataCenter } from './../datacenter';
|
||||
import { Workspace, WorkspaceMeta, WorkspaceType } from '../style';
|
||||
import { token } from './token';
|
||||
import { getDataCenter } from 'src';
|
||||
import { DataCenter } from 'src/datacenter';
|
||||
import { Workspace } from '../types';
|
||||
|
||||
export class Business {
|
||||
private _dc: DataCenter | undefined;
|
||||
// export class Business {
|
||||
// private _dc: DataCenter | undefined;
|
||||
|
||||
private async _getDc() {
|
||||
if (!this._dc) {
|
||||
this._dc = await getDataCenter();
|
||||
}
|
||||
return this._dc;
|
||||
}
|
||||
// private async _getDc() {
|
||||
// if (!this._dc) {
|
||||
// this._dc = await getDataCenter();
|
||||
// }
|
||||
// return this._dc;
|
||||
// }
|
||||
|
||||
async createWorkspace(
|
||||
name: string
|
||||
): Promise<Pick<Workspace, 'id' | 'name' | 'avatar' | 'type'>> {
|
||||
let id = '';
|
||||
let type = WorkspaceType.local;
|
||||
if (token.isLogin) {
|
||||
// TODO: add default avatar
|
||||
const data = await this._dc?.apis.createWorkspace({ name, avatar: '' });
|
||||
id = data?.id || '';
|
||||
type = WorkspaceType.cloud;
|
||||
this._dc?.load(id, { providerId: 'affine' });
|
||||
} else {
|
||||
this._dc?.load(uuidv4(), { providerId: 'local' });
|
||||
}
|
||||
const newWorkspaces = (await this.getWorkspaces()).find(w => w.id === id);
|
||||
return {
|
||||
id: newWorkspaces?.id || '',
|
||||
name,
|
||||
avatar: '',
|
||||
type,
|
||||
};
|
||||
}
|
||||
// // async createWorkspace(
|
||||
// // name: string
|
||||
// // ): Promise<Pick<Workspace, 'id' | 'name' | 'avatar' | 'type'>> {
|
||||
// // let id = '';
|
||||
// // let type = WorkspaceType.local;
|
||||
// // this._dc?.load(uuidv4());
|
||||
// // const newWorkspaces = (await this.getWorkspaces()).find(w => w.id === id);
|
||||
// // return {
|
||||
// // id: newWorkspaces?.id || '',
|
||||
// // name,
|
||||
// // avatar: '',
|
||||
// // type,
|
||||
// // };
|
||||
// // }
|
||||
|
||||
// not think out a good way to update workspace meta
|
||||
// updateWorkspaceMeta(
|
||||
// id: string,
|
||||
// meta: { name?: string; avatar: Partial<WorkspaceMeta> }
|
||||
// ) {}
|
||||
// // not think out a good way to update workspace meta
|
||||
// // updateWorkspaceMeta(
|
||||
// // id: string,
|
||||
// // meta: { name?: string; avatar: Partial<WorkspaceMeta> }
|
||||
// // ) {}
|
||||
|
||||
async getWorkspaces(focusUpdated?: boolean): Promise<Workspace[]> {
|
||||
const dc = await this._getDc();
|
||||
if (focusUpdated) {
|
||||
await dc.workspacesList.refreshWorkspaceList();
|
||||
}
|
||||
return dc.workspacesList.getWorkspaces();
|
||||
}
|
||||
// // async getWorkspaces(focusUpdated?: boolean): Promise<Workspace[]> {
|
||||
// // const dc = await this._getDc();
|
||||
// // if (focusUpdated) {
|
||||
// // await dc.workspacesList.refreshWorkspaceList();
|
||||
// // }
|
||||
// // return dc.workspacesList.getWorkspaces();
|
||||
// // }
|
||||
|
||||
/**
|
||||
* Get page list by workspace id
|
||||
* @param {string} id ID of workspace.
|
||||
*/
|
||||
getPagesByWorkspaceId(id: string) {
|
||||
return [];
|
||||
}
|
||||
// /**
|
||||
// * Get page list by workspace id
|
||||
// * @param {string} id ID of workspace.
|
||||
// */
|
||||
// getPagesByWorkspaceId(id: string) {
|
||||
// return [];
|
||||
// }
|
||||
|
||||
/**
|
||||
* Observe the update of the workspace
|
||||
* @param {function} callback({Workspace[]}).
|
||||
*/
|
||||
async onWorkspaceChange(cb: (workspaces: Workspace[]) => void) {
|
||||
const dc = await this._getDc();
|
||||
dc.workspacesList.on('change', cb);
|
||||
}
|
||||
// /**
|
||||
// * Observe the update of the workspace
|
||||
// * @param {function} callback({Workspace[]}).
|
||||
// */
|
||||
// async onWorkspaceChange(cb: (workspaces: Workspace[]) => void) {
|
||||
// const dc = await this._getDc();
|
||||
// dc.workspacesList.on('change', cb);
|
||||
// }
|
||||
|
||||
async deleteWorkspace(id: string) {
|
||||
const dc = await this._getDc();
|
||||
const workspace = dc.workspacesList.getWorkspaces().find(w => w.id === id);
|
||||
if (workspace?.type === WorkspaceType.cloud) {
|
||||
dc.apis.deleteWorkspace({ id });
|
||||
}
|
||||
dc.delete(id);
|
||||
}
|
||||
// async deleteWorkspace(id: string) {
|
||||
// const dc = await this._getDc();
|
||||
// const workspace = dc.workspacesList.getWorkspaces().find(w => w.id === id);
|
||||
// if (workspace?.type === WorkspaceType.cloud) {
|
||||
// dc.apis.deleteWorkspace({ id });
|
||||
// }
|
||||
// dc.delete(id);
|
||||
// }
|
||||
|
||||
/**
|
||||
* The member of the workspace go to leave workspace
|
||||
* @param {string} id ID of workspace.
|
||||
*/
|
||||
async leaveWorkspace(id: string) {
|
||||
const dc = await this._getDc();
|
||||
const workspace = dc.workspacesList.getWorkspaces().find(w => w.id === id);
|
||||
if (workspace?.type === WorkspaceType.cloud) {
|
||||
dc.apis.leaveWorkspace({ id });
|
||||
dc.delete(id);
|
||||
}
|
||||
}
|
||||
// /**
|
||||
// * The member of the workspace go to leave workspace
|
||||
// * @param {string} id ID of workspace.
|
||||
// */
|
||||
// async leaveWorkspace(id: string) {
|
||||
// const dc = await this._getDc();
|
||||
// const workspace = dc.workspacesList.getWorkspaces().find(w => w.id === id);
|
||||
// if (workspace?.type === WorkspaceType.cloud) {
|
||||
// dc.apis.leaveWorkspace({ id });
|
||||
// dc.delete(id);
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* Let the workspace to be public
|
||||
* @param {string} id ID of workspace.
|
||||
* @param {string} isPublish publish flag of workspace.
|
||||
*/
|
||||
setWorkspacePublish(id: string, isPublish: boolean): boolean {
|
||||
return isPublish;
|
||||
}
|
||||
// /**
|
||||
// * Let the workspace to be public
|
||||
// * @param {string} id ID of workspace.
|
||||
// * @param {string} isPublish publish flag of workspace.
|
||||
// */
|
||||
// setWorkspacePublish(id: string, isPublish: boolean): boolean {
|
||||
// return isPublish;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Get workspace by workspace id
|
||||
* @param {string} id ID of workspace.
|
||||
*/
|
||||
async getWorkspaceById(id: string) {
|
||||
const dc = await this._getDc();
|
||||
const workspace = dc.workspacesList.getWorkspaces().find(w => w.id === id);
|
||||
if (workspace?.type === WorkspaceType.cloud) {
|
||||
return dc.load(id, { providerId: 'affine' });
|
||||
} else {
|
||||
return dc.load(id, { providerId: 'local' });
|
||||
}
|
||||
}
|
||||
// /**
|
||||
// * Get workspace by workspace id
|
||||
// * @param {string} id ID of workspace.
|
||||
// */
|
||||
// async getWorkspaceById(id: string) {
|
||||
// const dc = await this._getDc();
|
||||
// const workspace = dc.workspacesList.getWorkspaces().find(w => w.id === id);
|
||||
// if (workspace?.type === WorkspaceType.cloud) {
|
||||
// return dc.load(id, { providerId: 'affine' });
|
||||
// } else {
|
||||
// return dc.load(id, { providerId: 'local' });
|
||||
// }
|
||||
// }
|
||||
|
||||
// no time
|
||||
/**
|
||||
* Get the members of the workspace
|
||||
* @param {string} id ID of workspace.
|
||||
*/
|
||||
getMembers(id: string): any {
|
||||
void 0;
|
||||
}
|
||||
/**
|
||||
* Add a new member to the workspace
|
||||
* @param {string} id ID of workspace.
|
||||
* @param {string} email new member email.
|
||||
*/
|
||||
inviteMember(id: string, email: string) {
|
||||
void 0;
|
||||
}
|
||||
// // no time
|
||||
// /**
|
||||
// * Get the members of the workspace
|
||||
// * @param {string} id ID of workspace.
|
||||
// */
|
||||
// getMembers(id: string): any {
|
||||
// void 0;
|
||||
// }
|
||||
// /**
|
||||
// * Add a new member to the workspace
|
||||
// * @param {string} id ID of workspace.
|
||||
// * @param {string} email new member email.
|
||||
// */
|
||||
// inviteMember(id: string, email: string) {
|
||||
// void 0;
|
||||
// }
|
||||
|
||||
/**
|
||||
* remove the new member to the workspace
|
||||
* @param {string} workspaceId ID of workspace.
|
||||
* @param {string} memberId ID of member
|
||||
*/
|
||||
removeMember(workspaceId: string, memberId: string) {
|
||||
void 0;
|
||||
}
|
||||
// /**
|
||||
// * remove the new member to the workspace
|
||||
// * @param {string} workspaceId ID of workspace.
|
||||
// * @param {string} memberId ID of member
|
||||
// */
|
||||
// removeMember(workspaceId: string, memberId: string) {
|
||||
// void 0;
|
||||
// }
|
||||
|
||||
/**
|
||||
* A new member click the invite link, finish to join the workspace
|
||||
* @param {string} inviteCode token for invitation.
|
||||
*/
|
||||
async acceptInvitation(invitingCode: string) {
|
||||
const dc = await this._getDc();
|
||||
dc.apis.acceptInviting({ invitingCode });
|
||||
}
|
||||
// /**
|
||||
// * A new member click the invite link, finish to join the workspace
|
||||
// * @param {string} inviteCode token for invitation.
|
||||
// */
|
||||
// async acceptInvitation(invitingCode: string) {
|
||||
// const dc = await this._getDc();
|
||||
// dc.apis.acceptInviting({ invitingCode });
|
||||
// }
|
||||
|
||||
// check with dark sky
|
||||
/**
|
||||
* Get login user info
|
||||
*/
|
||||
getUserInfo() {
|
||||
void 0;
|
||||
}
|
||||
// // check with dark sky
|
||||
// /**
|
||||
// * Get login user info
|
||||
// */
|
||||
// getUserInfo() {
|
||||
// void 0;
|
||||
// }
|
||||
|
||||
// TODO check with dark sky
|
||||
async login() {
|
||||
const dc = await this._getDc();
|
||||
await dc.auth('affine');
|
||||
}
|
||||
// // TODO check with dark sky
|
||||
// async login() {
|
||||
// const dc = await this._getDc();
|
||||
// await dc.auth('affine');
|
||||
// }
|
||||
|
||||
// just has no time
|
||||
/**
|
||||
* Logout and clear login session
|
||||
*/
|
||||
logout() {
|
||||
void 0;
|
||||
}
|
||||
// // just has no time
|
||||
// /**
|
||||
// * Logout and clear login session
|
||||
// */
|
||||
// logout() {
|
||||
// void 0;
|
||||
// }
|
||||
|
||||
// need discuss
|
||||
/**
|
||||
* Create a connection between local and cloud, sync cloud data to local
|
||||
* @param {string} id ID of workspace.
|
||||
* @param {string} id type of workspace.
|
||||
*/
|
||||
// setWorkspaceSyncType(id: string, type: 'local' | 'cloud') {}
|
||||
// // need discuss
|
||||
// /**
|
||||
// * Create a connection between local and cloud, sync cloud data to local
|
||||
// * @param {string} id ID of workspace.
|
||||
// * @param {string} id type of workspace.
|
||||
// */
|
||||
// // setWorkspaceSyncType(id: string, type: 'local' | 'cloud') {}
|
||||
|
||||
// need discuss
|
||||
/**
|
||||
* Select a file to import the workspace
|
||||
* @param {File} file file of workspace.
|
||||
*/
|
||||
importWorkspace(file: File) {
|
||||
void 0;
|
||||
}
|
||||
// // need discuss
|
||||
// /**
|
||||
// * Select a file to import the workspace
|
||||
// * @param {File} file file of workspace.
|
||||
// */
|
||||
// importWorkspace(file: File) {
|
||||
// void 0;
|
||||
// }
|
||||
|
||||
// need discuss may be not in apis
|
||||
// /**
|
||||
// * Generate a file ,and export it to local file system
|
||||
// * @param {string} id ID of workspace.
|
||||
// */
|
||||
exportWorkspace(id: string) {
|
||||
void 0;
|
||||
}
|
||||
// // need discuss may be not in apis
|
||||
// // /**
|
||||
// // * Generate a file ,and export it to local file system
|
||||
// // * @param {string} id ID of workspace.
|
||||
// // */
|
||||
// exportWorkspace(id: string) {
|
||||
// void 0;
|
||||
// }
|
||||
|
||||
// need discuss
|
||||
// /**
|
||||
// * Enable workspace cloud flag
|
||||
// * @param {string} id ID of workspace.
|
||||
// */
|
||||
enableWorkspaceCloud(id: string) {
|
||||
void 0;
|
||||
}
|
||||
}
|
||||
// // need discuss
|
||||
// // /**
|
||||
// // * Enable workspace cloud flag
|
||||
// // * @param {string} id ID of workspace.
|
||||
// // */
|
||||
// enableWorkspaceCloud(id: string) {
|
||||
// void 0;
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -168,14 +168,15 @@ export const getAuthorizer = () => {
|
||||
|
||||
const signInWithGoogle = async () => {
|
||||
const idToken = await getToken();
|
||||
let loginUser: AccessTokenMessage | null = null;
|
||||
if (idToken) {
|
||||
await token.initToken(idToken);
|
||||
loginUser = await token.initToken(idToken);
|
||||
} else {
|
||||
const user = await signInWithPopup(firebaseAuth, googleAuthProvider);
|
||||
const idToken = await user.user.getIdToken();
|
||||
await token.initToken(idToken);
|
||||
loginUser = await token.initToken(idToken);
|
||||
}
|
||||
return firebaseAuth.currentUser;
|
||||
return loginUser;
|
||||
};
|
||||
|
||||
const onAuthStateChanged = (callback: (user: User | null) => void) => {
|
||||
|
||||
234
packages/data-center/src/datacenter.bk.ts
Normal file
234
packages/data-center/src/datacenter.bk.ts
Normal file
@@ -0,0 +1,234 @@
|
||||
import assert from 'assert';
|
||||
import { BlockSchema } from '@blocksuite/blocks/models';
|
||||
import { Workspace, Signal } from '@blocksuite/store';
|
||||
|
||||
import { getLogger } from './index.js';
|
||||
import { getApis, Apis } from './apis/index.js';
|
||||
import { AffineProvider, BaseProvider } from './provider/index.js';
|
||||
import { LocalProvider } from './provider/index.js';
|
||||
import { getKVConfigure } from './store.js';
|
||||
import { Workspaces } from './workspaces';
|
||||
|
||||
// load workspace's config
|
||||
type LoadConfig = {
|
||||
// use witch provider load data
|
||||
providerId?: string;
|
||||
// provider config
|
||||
config?: Record<string, any>;
|
||||
};
|
||||
|
||||
export type DataCenterSignals = DataCenter['signals'];
|
||||
type WorkspaceItem = {
|
||||
// provider id
|
||||
provider: string;
|
||||
// data exists locally
|
||||
locally: boolean;
|
||||
};
|
||||
type WorkspaceLoadEvent = WorkspaceItem & {
|
||||
workspace: string;
|
||||
};
|
||||
|
||||
export class DataCenter {
|
||||
private readonly _apis: Apis;
|
||||
private readonly _providers = new Map<string, typeof BaseProvider>();
|
||||
private readonly _workspaces = new Map<string, Promise<BaseProvider>>();
|
||||
private readonly _config;
|
||||
private readonly _logger;
|
||||
public readonly workspacesList = new Workspaces(this);
|
||||
|
||||
readonly signals = {
|
||||
listAdd: new Signal<WorkspaceLoadEvent>(),
|
||||
listRemove: new Signal<string>(),
|
||||
};
|
||||
|
||||
static async init(debug: boolean): Promise<DataCenter> {
|
||||
const dc = new DataCenter(debug);
|
||||
dc.addProvider(AffineProvider);
|
||||
dc.addProvider(LocalProvider);
|
||||
|
||||
return dc;
|
||||
}
|
||||
|
||||
private constructor(debug: boolean) {
|
||||
this._apis = getApis();
|
||||
this._config = getKVConfigure('sys');
|
||||
this._logger = getLogger('dc');
|
||||
this._logger.enabled = debug;
|
||||
|
||||
this.signals.listAdd.on(e => {
|
||||
this._config.set(`list:${e.workspace}`, {
|
||||
provider: e.provider,
|
||||
locally: e.locally,
|
||||
});
|
||||
});
|
||||
this.signals.listRemove.on(workspace => {
|
||||
this._config.delete(`list:${workspace}`);
|
||||
});
|
||||
this.workspacesList.init();
|
||||
}
|
||||
|
||||
get apis(): Readonly<Apis> {
|
||||
return this._apis;
|
||||
}
|
||||
|
||||
private addProvider(provider: typeof BaseProvider) {
|
||||
this._providers.set(provider.id, provider);
|
||||
}
|
||||
|
||||
private async _getProvider(
|
||||
id: string,
|
||||
providerId = 'local'
|
||||
): Promise<string> {
|
||||
const providerKey = `${id}:provider`;
|
||||
if (this._providers.has(providerId)) {
|
||||
await this._config.set(providerKey, providerId);
|
||||
return providerId;
|
||||
} else {
|
||||
const providerValue = await this._config.get(providerKey);
|
||||
if (providerValue) return providerValue;
|
||||
}
|
||||
throw Error(`Provider ${providerId} not found`);
|
||||
}
|
||||
|
||||
private async _getWorkspace(
|
||||
id: string,
|
||||
params: LoadConfig
|
||||
): Promise<BaseProvider> {
|
||||
this._logger(`Init workspace ${id} with ${params.providerId}`);
|
||||
|
||||
const providerId = await this._getProvider(id, params.providerId);
|
||||
|
||||
// init workspace & register block schema
|
||||
const workspace = new Workspace({ room: id }).register(BlockSchema);
|
||||
|
||||
const Provider = this._providers.get(providerId);
|
||||
assert(Provider);
|
||||
|
||||
// initial configurator
|
||||
const config = getKVConfigure(`workspace:${id}`);
|
||||
// set workspace configs
|
||||
const values = Object.entries(params.config || {});
|
||||
if (values.length) await config.setMany(values);
|
||||
|
||||
// init data by provider
|
||||
const provider = new Provider();
|
||||
await provider.init({
|
||||
apis: this._apis,
|
||||
config,
|
||||
debug: this._logger.enabled,
|
||||
logger: this._logger.extend(`${Provider.id}:${id}`),
|
||||
signals: this.signals,
|
||||
workspace,
|
||||
});
|
||||
await provider.initData();
|
||||
this._logger(`Workspace ${id} loaded`);
|
||||
|
||||
return provider;
|
||||
}
|
||||
|
||||
async auth(providerId: string, globalConfig?: Record<string, any>) {
|
||||
const Provider = this._providers.get(providerId);
|
||||
if (Provider) {
|
||||
// initial configurator
|
||||
const config = getKVConfigure(`provider:${providerId}`);
|
||||
// set workspace configs
|
||||
const values = Object.entries(globalConfig || {});
|
||||
if (values.length) await config.setMany(values);
|
||||
|
||||
const logger = this._logger.extend(`auth:${providerId}`);
|
||||
logger.enabled = this._logger.enabled;
|
||||
await Provider.auth(config, logger, this.signals);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* load workspace data to memory
|
||||
* @param workspaceId workspace id
|
||||
* @param config.providerId provider id
|
||||
* @param config.config provider config
|
||||
* @returns Workspace instance
|
||||
*/
|
||||
async load(
|
||||
workspaceId: string,
|
||||
params: LoadConfig = {}
|
||||
): Promise<Workspace | null> {
|
||||
if (workspaceId) {
|
||||
if (!this._workspaces.has(workspaceId)) {
|
||||
this._workspaces.set(
|
||||
workspaceId,
|
||||
this._getWorkspace(workspaceId, params)
|
||||
);
|
||||
}
|
||||
const workspace = this._workspaces.get(workspaceId);
|
||||
assert(workspace);
|
||||
return workspace.then(w => w.workspace);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* destroy workspace's instance in memory
|
||||
* @param workspaceId workspace id
|
||||
*/
|
||||
async destroy(workspaceId: string) {
|
||||
const provider = await this._workspaces.get(workspaceId);
|
||||
if (provider) {
|
||||
this._workspaces.delete(workspaceId);
|
||||
await provider.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* reload new workspace instance to memory to refresh config
|
||||
* @param workspaceId workspace id
|
||||
* @param config.providerId provider id
|
||||
* @param config.config provider config
|
||||
* @returns Workspace instance
|
||||
*/
|
||||
async reload(
|
||||
workspaceId: string,
|
||||
config: LoadConfig = {}
|
||||
): Promise<Workspace | null> {
|
||||
await this.destroy(workspaceId);
|
||||
return this.load(workspaceId, config);
|
||||
}
|
||||
|
||||
/**
|
||||
* get workspace list,return a map of workspace id and data state
|
||||
* data state is also map, the key is the provider id, and the data exists locally when the value is true, otherwise it does not exist
|
||||
*/
|
||||
async list(): Promise<Record<string, Record<string, boolean>>> {
|
||||
const listString = 'list:';
|
||||
const entries: [string, WorkspaceItem][] = await this._config.entries();
|
||||
return entries.reduce((acc, [k, i]) => {
|
||||
if (k.startsWith(listString)) {
|
||||
const key = k.slice(listString.length);
|
||||
acc[key] = acc[key] || {};
|
||||
acc[key][i.provider] = i.locally;
|
||||
}
|
||||
return acc;
|
||||
}, {} as Record<string, Record<string, boolean>>);
|
||||
}
|
||||
|
||||
/**
|
||||
* delete local workspace's data
|
||||
* @param workspaceId workspace id
|
||||
*/
|
||||
async delete(workspaceId: string) {
|
||||
await this._config.delete(`${workspaceId}:provider`);
|
||||
const provider = await this._workspaces.get(workspaceId);
|
||||
if (provider) {
|
||||
this._workspaces.delete(workspaceId);
|
||||
// clear workspace data implement by provider
|
||||
await provider.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* clear all local workspace's data
|
||||
*/
|
||||
async clear() {
|
||||
const workspaces = await this.list();
|
||||
await Promise.all(Object.keys(workspaces).map(id => this.delete(id)));
|
||||
}
|
||||
}
|
||||
@@ -1,234 +1,275 @@
|
||||
import assert from 'assert';
|
||||
import { BlockSchema } from '@blocksuite/blocks/models';
|
||||
import { Workspace, Signal } from '@blocksuite/store';
|
||||
|
||||
import { getLogger } from './index.js';
|
||||
import { getApis, Apis } from './apis/index.js';
|
||||
import { AffineProvider, BaseProvider } from './provider/index.js';
|
||||
import { LocalProvider } from './provider/index.js';
|
||||
import { getKVConfigure } from './store.js';
|
||||
import { Workspaces } from './workspaces';
|
||||
import { Workspace } from '@blocksuite/store';
|
||||
import { BaseProvider } from './provider/base';
|
||||
import { LocalProvider } from './provider/local/local';
|
||||
import { AffineProvider } from './provider';
|
||||
import { Workspace as WS, WorkspaceMeta } from 'src/types';
|
||||
import assert from 'assert';
|
||||
import { getLogger } from 'src';
|
||||
import { BlockSchema } from '@blocksuite/blocks/models';
|
||||
import { applyUpdate, encodeStateAsUpdate } from 'yjs';
|
||||
|
||||
// load workspace's config
|
||||
type LoadConfig = {
|
||||
// use witch provider load data
|
||||
providerId?: string;
|
||||
// provider config
|
||||
config?: Record<string, any>;
|
||||
};
|
||||
|
||||
export type DataCenterSignals = DataCenter['signals'];
|
||||
type WorkspaceItem = {
|
||||
// provider id
|
||||
provider: string;
|
||||
// data exists locally
|
||||
locally: boolean;
|
||||
};
|
||||
type WorkspaceLoadEvent = WorkspaceItem & {
|
||||
workspace: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* @class DataCenter
|
||||
* @classdesc DataCenter is a data center, it can manage different providers for business
|
||||
*/
|
||||
export class DataCenter {
|
||||
private readonly _apis: Apis;
|
||||
private readonly _providers = new Map<string, typeof BaseProvider>();
|
||||
private readonly _workspaces = new Map<string, Promise<BaseProvider>>();
|
||||
private readonly _config;
|
||||
private readonly _logger;
|
||||
public readonly workspacesList = new Workspaces(this);
|
||||
private readonly workspaces = new Workspaces(this);
|
||||
private currentWorkspace: Workspace | null = null;
|
||||
private readonly _logger = getLogger('dc');
|
||||
providerMap: Map<string, BaseProvider> = new Map();
|
||||
|
||||
readonly signals = {
|
||||
listAdd: new Signal<WorkspaceLoadEvent>(),
|
||||
listRemove: new Signal<string>(),
|
||||
};
|
||||
constructor(debug: boolean) {
|
||||
this._logger.enabled = debug;
|
||||
}
|
||||
|
||||
static async init(debug: boolean): Promise<DataCenter> {
|
||||
const dc = new DataCenter(debug);
|
||||
dc.addProvider(AffineProvider);
|
||||
dc.addProvider(LocalProvider);
|
||||
// TODO: switch different provider
|
||||
dc.registerProvider(new LocalProvider());
|
||||
dc.registerProvider(new AffineProvider());
|
||||
dc.workspaces.init();
|
||||
|
||||
return dc;
|
||||
}
|
||||
|
||||
private constructor(debug: boolean) {
|
||||
this._apis = getApis();
|
||||
this._config = getKVConfigure('sys');
|
||||
this._logger = getLogger('dc');
|
||||
this._logger.enabled = debug;
|
||||
|
||||
this.signals.listAdd.on(e => {
|
||||
this._config.set(`list:${e.workspace}`, {
|
||||
provider: e.provider,
|
||||
locally: e.locally,
|
||||
});
|
||||
});
|
||||
this.signals.listRemove.on(workspace => {
|
||||
this._config.delete(`list:${workspace}`);
|
||||
});
|
||||
this.workspacesList.init();
|
||||
registerProvider(provider: BaseProvider) {
|
||||
// inject data in provider
|
||||
provider.inject({ logger: this._logger, workspaces: this.workspaces });
|
||||
provider.init();
|
||||
this.providerMap.set(provider.id, provider);
|
||||
}
|
||||
|
||||
get apis(): Readonly<Apis> {
|
||||
return this._apis;
|
||||
get providers() {
|
||||
return Array.from(this.providerMap.values());
|
||||
}
|
||||
|
||||
private addProvider(provider: typeof BaseProvider) {
|
||||
this._providers.set(provider.id, provider);
|
||||
/**
|
||||
* create new workspace , new workspace is a local workspace
|
||||
* @param {string} name workspace name
|
||||
* @returns
|
||||
*/
|
||||
public async createWorkspace(name: string) {
|
||||
const workspaceInfo = this.workspaces.addLocalWorkspace(name);
|
||||
return workspaceInfo;
|
||||
}
|
||||
|
||||
private async _getProvider(
|
||||
id: string,
|
||||
providerId = 'local'
|
||||
): Promise<string> {
|
||||
const providerKey = `${id}:provider`;
|
||||
if (this._providers.has(providerId)) {
|
||||
await this._config.set(providerKey, providerId);
|
||||
return providerId;
|
||||
} else {
|
||||
const providerValue = await this._config.get(providerKey);
|
||||
if (providerValue) return providerValue;
|
||||
}
|
||||
throw Error(`Provider ${providerId} not found`);
|
||||
}
|
||||
|
||||
private async _getWorkspace(
|
||||
id: string,
|
||||
params: LoadConfig
|
||||
): Promise<BaseProvider> {
|
||||
this._logger(`Init workspace ${id} with ${params.providerId}`);
|
||||
|
||||
const providerId = await this._getProvider(id, params.providerId);
|
||||
|
||||
// init workspace & register block schema
|
||||
const workspace = new Workspace({ room: id }).register(BlockSchema);
|
||||
|
||||
const Provider = this._providers.get(providerId);
|
||||
assert(Provider);
|
||||
|
||||
// initial configurator
|
||||
const config = getKVConfigure(`workspace:${id}`);
|
||||
// set workspace configs
|
||||
const values = Object.entries(params.config || {});
|
||||
if (values.length) await config.setMany(values);
|
||||
|
||||
// init data by provider
|
||||
const provider = new Provider();
|
||||
await provider.init({
|
||||
apis: this._apis,
|
||||
config,
|
||||
debug: this._logger.enabled,
|
||||
logger: this._logger.extend(`${Provider.id}:${id}`),
|
||||
signals: this.signals,
|
||||
workspace,
|
||||
});
|
||||
await provider.initData();
|
||||
this._logger(`Workspace ${id} loaded`);
|
||||
|
||||
return provider;
|
||||
}
|
||||
|
||||
async auth(providerId: string, globalConfig?: Record<string, any>) {
|
||||
const Provider = this._providers.get(providerId);
|
||||
if (Provider) {
|
||||
// initial configurator
|
||||
const config = getKVConfigure(`provider:${providerId}`);
|
||||
// set workspace configs
|
||||
const values = Object.entries(globalConfig || {});
|
||||
if (values.length) await config.setMany(values);
|
||||
|
||||
const logger = this._logger.extend(`auth:${providerId}`);
|
||||
logger.enabled = this._logger.enabled;
|
||||
await Provider.auth(config, logger, this.signals);
|
||||
/**
|
||||
* delete workspace by id
|
||||
* @param {string} workspaceId workspace id
|
||||
*/
|
||||
public async deleteWorkspace(workspaceId: string) {
|
||||
const workspaceInfo = this.workspaces.getWorkspace(workspaceId);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||
if (provider && this.workspaces.hasWorkspace(workspaceId)) {
|
||||
await provider.delete(workspaceId);
|
||||
// may be refresh all workspaces
|
||||
this.workspaces.delete(workspaceId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* load workspace data to memory
|
||||
* @param workspaceId workspace id
|
||||
* @param config.providerId provider id
|
||||
* @param config.config provider config
|
||||
* @returns Workspace instance
|
||||
* get a new workspace only has room id
|
||||
* @param {string} workspaceId workspace id
|
||||
*/
|
||||
async load(
|
||||
workspaceId: string,
|
||||
params: LoadConfig = {}
|
||||
): Promise<Workspace | null> {
|
||||
if (workspaceId) {
|
||||
if (!this._workspaces.has(workspaceId)) {
|
||||
this._workspaces.set(
|
||||
workspaceId,
|
||||
this._getWorkspace(workspaceId, params)
|
||||
);
|
||||
}
|
||||
const workspace = this._workspaces.get(workspaceId);
|
||||
assert(workspace);
|
||||
return workspace.then(w => w.workspace);
|
||||
}
|
||||
return null;
|
||||
private _getWorkspace(workspaceId: string) {
|
||||
return new Workspace({
|
||||
room: workspaceId,
|
||||
}).register(BlockSchema);
|
||||
}
|
||||
|
||||
/**
|
||||
* destroy workspace's instance in memory
|
||||
* @param workspaceId workspace id
|
||||
* login to all providers, it will default run all auth ,
|
||||
* maybe need a params to control which provider to auth
|
||||
*/
|
||||
async destroy(workspaceId: string) {
|
||||
const provider = await this._workspaces.get(workspaceId);
|
||||
public async login() {
|
||||
this.providers.forEach(p => {
|
||||
// TODO: may be add params of auth
|
||||
p.auth();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* logout from all providers
|
||||
*/
|
||||
public async logout() {
|
||||
this.providers.forEach(p => {
|
||||
p.logout();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* load workspace instance by id
|
||||
* @param {string} workspaceId workspace id
|
||||
*/
|
||||
public async loadWorkspace(workspaceId: string) {
|
||||
const workspaceInfo = this.workspaces.getWorkspace(workspaceId);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
const currentProvider = this.providerMap.get(workspaceInfo.provider);
|
||||
if (currentProvider) {
|
||||
currentProvider.close(workspaceId);
|
||||
}
|
||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||
if (provider) {
|
||||
this._workspaces.delete(workspaceId);
|
||||
await provider.destroy();
|
||||
this._logger(`Loading ${provider} workspace: `, workspaceId);
|
||||
const workspace = this._getWorkspace(workspaceId);
|
||||
this.currentWorkspace = await provider.warpWorkspace(workspace);
|
||||
return this.currentWorkspace;
|
||||
}
|
||||
return workspaceInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* reload new workspace instance to memory to refresh config
|
||||
* @param workspaceId workspace id
|
||||
* @param config.providerId provider id
|
||||
* @param config.config provider config
|
||||
* @returns Workspace instance
|
||||
* get user info by provider id
|
||||
* @param {string} providerId the provider name of workspace
|
||||
*/
|
||||
async reload(
|
||||
workspaceId: string,
|
||||
config: LoadConfig = {}
|
||||
): Promise<Workspace | null> {
|
||||
await this.destroy(workspaceId);
|
||||
return this.load(workspaceId, config);
|
||||
public async getUserInfo(providerId = 'affine') {
|
||||
// XXX: maybe return all user info
|
||||
const provider = this.providerMap.get(providerId);
|
||||
assert(provider, 'Provider not found');
|
||||
return provider.getUserInfo();
|
||||
}
|
||||
|
||||
/**
|
||||
* get workspace list,return a map of workspace id and data state
|
||||
* data state is also map, the key is the provider id, and the data exists locally when the value is true, otherwise it does not exist
|
||||
* listen workspaces list change
|
||||
*/
|
||||
async list(): Promise<Record<string, Record<string, boolean>>> {
|
||||
const listString = 'list:';
|
||||
const entries: [string, WorkspaceItem][] = await this._config.entries();
|
||||
return entries.reduce((acc, [k, i]) => {
|
||||
if (k.startsWith(listString)) {
|
||||
const key = k.slice(listString.length);
|
||||
acc[key] = acc[key] || {};
|
||||
acc[key][i.provider] = i.locally;
|
||||
}
|
||||
return acc;
|
||||
}, {} as Record<string, Record<string, boolean>>);
|
||||
public async onWorkspacesChange(callback: (workspaces: WS[]) => void) {
|
||||
this.workspaces.onWorkspacesChange(callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* delete local workspace's data
|
||||
* @param workspaceId workspace id
|
||||
* change workspaces meta
|
||||
* @param {WorkspaceMeta} workspaceMeta workspace meta
|
||||
* @param {Workspace} workspace workspace instance
|
||||
*/
|
||||
async delete(workspaceId: string) {
|
||||
await this._config.delete(`${workspaceId}:provider`);
|
||||
const provider = await this._workspaces.get(workspaceId);
|
||||
public async resetWorkspaceMeta(
|
||||
{ name, avatar }: WorkspaceMeta,
|
||||
workspace?: Workspace
|
||||
) {
|
||||
const w = workspace ?? this.currentWorkspace;
|
||||
assert(w?.room, 'No workspace to set meta');
|
||||
const update: Partial<WorkspaceMeta> = {};
|
||||
if (name) {
|
||||
w.doc.meta.setName(name);
|
||||
update.name = name;
|
||||
}
|
||||
if (avatar) {
|
||||
w.doc.meta.setAvatar(avatar);
|
||||
update.avatar = avatar;
|
||||
}
|
||||
this.workspaces.updateWorkspaceMeta(w.room, update);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* leave workspace by id
|
||||
* @param id workspace id
|
||||
*/
|
||||
public async leaveWorkspace(workspaceId: string) {
|
||||
const workspaceInfo = this.workspaces.getWorkspace(workspaceId);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||
if (provider) {
|
||||
this._workspaces.delete(workspaceId);
|
||||
// clear workspace data implement by provider
|
||||
await provider.clear();
|
||||
provider.close(workspaceId);
|
||||
provider.leave(workspaceId);
|
||||
}
|
||||
}
|
||||
|
||||
public async setWorkspacePublish() {
|
||||
// TODO: set workspace publish
|
||||
}
|
||||
|
||||
public async inviteMember(id: string, email: string) {
|
||||
const workspaceInfo = this.workspaces.getWorkspace(id);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||
if (provider) {
|
||||
provider.invite(id, email);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* clear all local workspace's data
|
||||
* remove the new member to the workspace
|
||||
* @param {number} permissionId permission id
|
||||
*/
|
||||
async clear() {
|
||||
const workspaces = await this.list();
|
||||
await Promise.all(Object.keys(workspaces).map(id => this.delete(id)));
|
||||
public async removeMember(workspaceId: string, permissionId: number) {
|
||||
const workspaceInfo = this.workspaces.getWorkspace(workspaceId);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||
if (provider) {
|
||||
provider.removeMember(permissionId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* do close current workspace
|
||||
*/
|
||||
public async closeCurrentWorkspace() {
|
||||
assert(this.currentWorkspace?.room, 'No workspace to close');
|
||||
const currentWorkspace = this.workspaces.getWorkspace(
|
||||
this.currentWorkspace.room
|
||||
);
|
||||
assert(currentWorkspace, 'Workspace not found');
|
||||
const provider = this.providerMap.get(currentWorkspace.provider);
|
||||
assert(provider, 'Provider not found');
|
||||
provider.close(currentWorkspace.id);
|
||||
}
|
||||
|
||||
private async _transWorkspaceProvider(
|
||||
workspace: Workspace,
|
||||
provider: string
|
||||
) {
|
||||
assert(workspace.room, 'No workspace');
|
||||
const workspaceInfo = this.workspaces.getWorkspace(workspace.room);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
if (workspaceInfo.provider === provider) {
|
||||
this._logger('Workspace provider is same');
|
||||
return;
|
||||
}
|
||||
const currentProvider = this.providerMap.get(workspaceInfo.provider);
|
||||
assert(currentProvider, 'Provider not found');
|
||||
const newProvider = this.providerMap.get(provider);
|
||||
assert(newProvider, 'AffineProvider is not registered');
|
||||
const newWorkspace = await newProvider.createWorkspace({
|
||||
name: workspaceInfo.name,
|
||||
avatar: workspaceInfo.avatar,
|
||||
});
|
||||
assert(newWorkspace, 'Create workspace failed');
|
||||
// load doc to another workspace
|
||||
applyUpdate(newWorkspace.doc, encodeStateAsUpdate(workspace.doc));
|
||||
assert(newWorkspace, 'Create workspace failed');
|
||||
currentProvider.delete(workspace.room);
|
||||
this.workspaces.refreshWorkspaces();
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable workspace cloud flag
|
||||
* @param {string} id ID of workspace.
|
||||
*/
|
||||
public async enableWorkspaceCloud(
|
||||
workspace: Workspace | null = this.currentWorkspace
|
||||
) {
|
||||
assert(workspace?.room, 'No workspace to enable cloud');
|
||||
return await this._transWorkspaceProvider(workspace, 'affine');
|
||||
}
|
||||
|
||||
/**
|
||||
* Select a file to import the workspace
|
||||
* @param {File} file file of workspace.
|
||||
*/
|
||||
public async importWorkspace(file: File) {
|
||||
file;
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a file ,and export it to local file system
|
||||
* @param {string} id ID of workspace.
|
||||
*/
|
||||
public async exportWorkspace(id: string) {
|
||||
id;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import debug from 'debug';
|
||||
import { DataCenter } from './datacenter.js';
|
||||
import { DataCenter } from './dataCenter';
|
||||
|
||||
const _initializeDataCenter = () => {
|
||||
let _dataCenterInstance: Promise<DataCenter>;
|
||||
@@ -32,5 +32,5 @@ export function getLogger(namespace: string) {
|
||||
return logger;
|
||||
}
|
||||
|
||||
export type { AccessTokenMessage, Member, Workspace } from './apis';
|
||||
export { WorkspaceType } from './apis/index.js';
|
||||
export type { AccessTokenMessage } from './apis';
|
||||
export type { Workspace } from './types';
|
||||
|
||||
238
packages/data-center/src/provider/affine/affine.ts
Normal file
238
packages/data-center/src/provider/affine/affine.ts
Normal file
@@ -0,0 +1,238 @@
|
||||
import {
|
||||
getWorkspaces,
|
||||
getWorkspaceDetail,
|
||||
WorkspaceDetail,
|
||||
downloadWorkspace,
|
||||
deleteWorkspace,
|
||||
leaveWorkspace,
|
||||
inviteMember,
|
||||
removeMember,
|
||||
createWorkspace,
|
||||
} from 'src/apis/workspace';
|
||||
import { BaseProvider } from '../base';
|
||||
import { User, Workspace as WS, WorkspaceMeta } from 'src/types';
|
||||
import { Workspace } from '@blocksuite/store';
|
||||
import { BlockSchema } from '@blocksuite/blocks/models';
|
||||
import { applyUpdate } from 'yjs';
|
||||
import { token, Callback } from 'src/apis';
|
||||
import { varStorage as storage } from 'lib0/storage';
|
||||
import assert from 'assert';
|
||||
import { getAuthorizer } from 'src/apis/token';
|
||||
import { WebsocketProvider } from 'src/provider/sync';
|
||||
import { IndexedDBProvider } from '../indexeddb';
|
||||
|
||||
export class AffineProvider extends BaseProvider {
|
||||
public id = 'affine';
|
||||
private _workspacesCache: Map<string, Workspace> = new Map();
|
||||
private _onTokenRefresh?: Callback = undefined;
|
||||
private readonly _authorizer = getAuthorizer();
|
||||
private _user: User | undefined = undefined;
|
||||
private _wsMap: Map<string, WebsocketProvider> = new Map();
|
||||
private _idbMap: Map<string, IndexedDBProvider> = new Map();
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
override async init() {
|
||||
this._onTokenRefresh = () => {
|
||||
if (token.refresh) {
|
||||
storage.setItem('token', token.refresh);
|
||||
}
|
||||
};
|
||||
|
||||
token.onChange(this._onTokenRefresh);
|
||||
|
||||
// initial login token
|
||||
if (token.isExpired) {
|
||||
try {
|
||||
const refreshToken = storage.getItem('token');
|
||||
await token.refreshToken(refreshToken);
|
||||
|
||||
if (token.refresh) {
|
||||
storage.set('token', token.refresh);
|
||||
}
|
||||
|
||||
assert(token.isLogin);
|
||||
} catch (_) {
|
||||
// this._logger('Authorization failed, fallback to local mode');
|
||||
}
|
||||
} else {
|
||||
storage.setItem('token', token.refresh);
|
||||
}
|
||||
}
|
||||
|
||||
override async warpWorkspace(workspace: Workspace) {
|
||||
const { doc, room } = workspace;
|
||||
assert(room);
|
||||
this._initWorkspaceDb(workspace);
|
||||
const updates = await downloadWorkspace(room);
|
||||
if (updates) {
|
||||
await new Promise(resolve => {
|
||||
doc.once('update', resolve);
|
||||
applyUpdate(doc, new Uint8Array(updates));
|
||||
});
|
||||
}
|
||||
const ws = new WebsocketProvider('/', room, doc);
|
||||
this._wsMap.set(room, ws);
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
// TODO: synced will also be triggered on reconnection after losing sync
|
||||
// There needs to be an event mechanism to emit the synchronization state to the upper layer
|
||||
assert(ws);
|
||||
ws.once('synced', () => resolve());
|
||||
ws.once('lost-connection', () => resolve());
|
||||
ws.once('connection-error', () => reject());
|
||||
});
|
||||
return workspace;
|
||||
}
|
||||
|
||||
override async loadWorkspaces() {
|
||||
if (!token.isLogin) {
|
||||
return [];
|
||||
}
|
||||
const workspacesList = await getWorkspaces();
|
||||
const workspaces: WS[] = workspacesList.map(w => {
|
||||
return {
|
||||
...w,
|
||||
memberCount: 0,
|
||||
name: '',
|
||||
provider: 'affine',
|
||||
};
|
||||
});
|
||||
const workspaceInstances = workspaces.map(({ id }) => {
|
||||
const workspace =
|
||||
this._workspacesCache.get(id) ||
|
||||
new Workspace({
|
||||
room: id,
|
||||
}).register(BlockSchema);
|
||||
this._workspacesCache.set(id, workspace);
|
||||
if (workspace) {
|
||||
return new Promise<Workspace>(resolve => {
|
||||
downloadWorkspace(id).then(data => {
|
||||
applyUpdate(workspace.doc, new Uint8Array(data));
|
||||
resolve(workspace);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
});
|
||||
await (
|
||||
await Promise.all(workspaceInstances)
|
||||
).forEach((workspace, i) => {
|
||||
if (workspace) {
|
||||
workspaces[i] = {
|
||||
...workspaces[i],
|
||||
name: workspace.doc.meta.name,
|
||||
avatar: workspace.doc.meta.avatar,
|
||||
};
|
||||
}
|
||||
});
|
||||
const getDetailList = workspacesList.map(w => {
|
||||
const { id } = w;
|
||||
return new Promise<{ id: string; detail: WorkspaceDetail | null }>(
|
||||
resolve => {
|
||||
getWorkspaceDetail({ id }).then(data => {
|
||||
resolve({ id, detail: data || null });
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
const ownerList = await Promise.all(getDetailList);
|
||||
(await Promise.all(ownerList)).forEach(detail => {
|
||||
if (detail) {
|
||||
const { id, detail: workspaceDetail } = detail;
|
||||
if (workspaceDetail) {
|
||||
const { owner, member_count } = workspaceDetail;
|
||||
const currentWorkspace = workspaces.find(w => w.id === id);
|
||||
if (currentWorkspace) {
|
||||
currentWorkspace.owner = {
|
||||
id: owner.id,
|
||||
name: owner.name,
|
||||
avatar: owner.avatar_url,
|
||||
email: owner.email,
|
||||
};
|
||||
currentWorkspace.memberCount = member_count;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return workspaces;
|
||||
}
|
||||
|
||||
override async auth() {
|
||||
const refreshToken = await storage.getItem('token');
|
||||
if (refreshToken) {
|
||||
await token.refreshToken(refreshToken);
|
||||
if (token.isLogin && !token.isExpired) {
|
||||
// login success
|
||||
return;
|
||||
}
|
||||
}
|
||||
const user = await this._authorizer[0]?.();
|
||||
assert(user);
|
||||
this._user = {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
avatar: user.avatar_url,
|
||||
email: user.email,
|
||||
};
|
||||
}
|
||||
|
||||
public override async getUserInfo(): Promise<User | undefined> {
|
||||
return this._user;
|
||||
}
|
||||
|
||||
public override async delete(id: string): Promise<void> {
|
||||
// TODO delete workspace all local data
|
||||
await deleteWorkspace({ id });
|
||||
}
|
||||
|
||||
public override async clear(): Promise<void> {
|
||||
// TODO: clear all workspaces source
|
||||
this._workspacesCache.clear();
|
||||
}
|
||||
|
||||
public override async close(id: string) {
|
||||
const idb = this._idbMap.get(id);
|
||||
idb?.destroy();
|
||||
const ws = this._wsMap.get(id);
|
||||
ws?.disconnect();
|
||||
}
|
||||
|
||||
public override async leave(id: string): Promise<void> {
|
||||
await leaveWorkspace({ id });
|
||||
}
|
||||
|
||||
public override async invite(id: string, email: string): Promise<void> {
|
||||
return await inviteMember({ id, email });
|
||||
}
|
||||
|
||||
public override async removeMember(permissionId: number): Promise<void> {
|
||||
return await removeMember({ permissionId });
|
||||
}
|
||||
|
||||
private async _initWorkspaceDb(workspace: Workspace) {
|
||||
assert(workspace.room);
|
||||
let idb = this._idbMap.get(workspace.room);
|
||||
idb?.destroy();
|
||||
idb = new IndexedDBProvider(workspace.room, workspace.doc);
|
||||
this._idbMap.set(workspace.room, idb);
|
||||
await idb.whenSynced;
|
||||
return idb;
|
||||
}
|
||||
|
||||
public override async createWorkspace(
|
||||
meta: WorkspaceMeta
|
||||
): Promise<Workspace | undefined> {
|
||||
assert(meta.name, 'Workspace name is required');
|
||||
meta.avatar ?? (meta.avatar = '');
|
||||
const { id } = await createWorkspace(meta as Required<WorkspaceMeta>);
|
||||
const nw = new Workspace({
|
||||
room: id,
|
||||
}).register(BlockSchema);
|
||||
this._initWorkspaceDb(nw);
|
||||
this._logger('Local data loaded');
|
||||
return nw;
|
||||
}
|
||||
}
|
||||
@@ -1,175 +1 @@
|
||||
import assert from 'assert';
|
||||
import { applyUpdate, Doc } from 'yjs';
|
||||
|
||||
import type {
|
||||
ConfigStore,
|
||||
DataCenterSignals,
|
||||
InitialParams,
|
||||
Logger,
|
||||
} from '../index.js';
|
||||
import { token, Callback, getApis } from '../../apis/index.js';
|
||||
import { LocalProvider } from '../local/index.js';
|
||||
|
||||
import { WebsocketProvider } from './sync.js';
|
||||
import { IndexedDBProvider } from '../local/indexeddb.js';
|
||||
|
||||
export class AffineProvider extends LocalProvider {
|
||||
static id = 'affine';
|
||||
private _onTokenRefresh?: Callback = undefined;
|
||||
private _ws?: WebsocketProvider;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
async init(params: InitialParams) {
|
||||
super.init(params);
|
||||
|
||||
this._onTokenRefresh = () => {
|
||||
if (token.refresh) {
|
||||
this._config.set('token', token.refresh);
|
||||
}
|
||||
};
|
||||
assert(this._onTokenRefresh);
|
||||
|
||||
token.onChange(this._onTokenRefresh);
|
||||
|
||||
// initial login token
|
||||
if (token.isExpired) {
|
||||
try {
|
||||
const refreshToken = await this._config.get('token');
|
||||
await token.refreshToken(refreshToken);
|
||||
|
||||
if (token.refresh) {
|
||||
this._config.set('token', token.refresh);
|
||||
}
|
||||
|
||||
assert(token.isLogin);
|
||||
} catch (_) {
|
||||
this._logger('Authorization failed, fallback to local mode');
|
||||
}
|
||||
} else {
|
||||
this._config.set('token', token.refresh);
|
||||
}
|
||||
}
|
||||
|
||||
async destroy() {
|
||||
if (this._onTokenRefresh) {
|
||||
token.offChange(this._onTokenRefresh);
|
||||
}
|
||||
this._ws?.disconnect();
|
||||
}
|
||||
|
||||
async initData() {
|
||||
const databases = await indexedDB.databases();
|
||||
await super.initData(
|
||||
// set locally to true if exists a same name db
|
||||
databases
|
||||
.map(db => db.name)
|
||||
.filter(v => v)
|
||||
.includes(this._workspace.room)
|
||||
);
|
||||
|
||||
const workspace = this._workspace;
|
||||
const doc = workspace.doc;
|
||||
|
||||
this._logger(`Login: ${token.isLogin}`);
|
||||
|
||||
if (workspace.room && token.isLogin) {
|
||||
try {
|
||||
// init data from cloud
|
||||
await AffineProvider._initCloudDoc(
|
||||
workspace.room,
|
||||
doc,
|
||||
this._logger,
|
||||
this._signals
|
||||
);
|
||||
|
||||
// Wait for ws synchronization to complete, otherwise the data will be modified in reverse, which can be optimized later
|
||||
this._ws = new WebsocketProvider('/', workspace.room, doc);
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
// TODO: synced will also be triggered on reconnection after losing sync
|
||||
// There needs to be an event mechanism to emit the synchronization state to the upper layer
|
||||
assert(this._ws);
|
||||
this._ws.once('synced', () => resolve());
|
||||
this._ws.once('lost-connection', () => resolve());
|
||||
this._ws.once('connection-error', () => reject());
|
||||
});
|
||||
this._signals.listAdd.emit({
|
||||
workspace: workspace.room,
|
||||
provider: this.id,
|
||||
locally: true,
|
||||
});
|
||||
} catch (e) {
|
||||
this._logger('Failed to init cloud workspace', e);
|
||||
}
|
||||
}
|
||||
|
||||
// if after update, the space:meta is empty
|
||||
// then we need to get map with doc
|
||||
// just a workaround for yjs
|
||||
doc.getMap('space:meta');
|
||||
}
|
||||
|
||||
private static async _initCloudDoc(
|
||||
workspace: string,
|
||||
doc: Doc,
|
||||
logger: Logger,
|
||||
signals: DataCenterSignals
|
||||
) {
|
||||
const apis = getApis();
|
||||
logger(`Loading ${workspace}...`);
|
||||
const updates = await apis.downloadWorkspace(workspace);
|
||||
if (updates) {
|
||||
await new Promise(resolve => {
|
||||
doc.once('update', resolve);
|
||||
applyUpdate(doc, new Uint8Array(updates));
|
||||
});
|
||||
logger(`Loaded: ${workspace}`);
|
||||
|
||||
// only add to list as online workspace
|
||||
signals.listAdd.emit({
|
||||
workspace,
|
||||
provider: this.id,
|
||||
// at this time we always download full workspace
|
||||
// but after we support sub doc, we can only download metadata
|
||||
locally: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static async auth(
|
||||
config: Readonly<ConfigStore<string>>,
|
||||
logger: Logger,
|
||||
signals: DataCenterSignals
|
||||
) {
|
||||
const refreshToken = await config.get('token');
|
||||
if (refreshToken) {
|
||||
await token.refreshToken(refreshToken);
|
||||
if (token.isLogin && !token.isExpired) {
|
||||
logger('check login success');
|
||||
// login success
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
logger('start login');
|
||||
// login with google
|
||||
const apis = getApis();
|
||||
assert(apis.signInWithGoogle);
|
||||
const user = await apis.signInWithGoogle();
|
||||
assert(user);
|
||||
logger(`login success: ${user.displayName}`);
|
||||
|
||||
// TODO: refresh local workspace data
|
||||
const workspaces = await apis.getWorkspaces();
|
||||
await Promise.all(
|
||||
workspaces.map(async ({ id }) => {
|
||||
const doc = new Doc();
|
||||
const idb = new IndexedDBProvider(id, doc);
|
||||
await idb.whenSynced;
|
||||
await this._initCloudDoc(id, doc, logger, signals);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
export * from './affine';
|
||||
|
||||
@@ -1,79 +1,134 @@
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import type { Workspace } from '@blocksuite/store';
|
||||
|
||||
import type {
|
||||
Apis,
|
||||
DataCenterSignals,
|
||||
Logger,
|
||||
InitialParams,
|
||||
ConfigStore,
|
||||
} from './index';
|
||||
import { BlobStorage, Workspace } from '@blocksuite/store';
|
||||
import { Logger, User, Workspace as WS, WorkspaceMeta } from 'src/types';
|
||||
import { Workspaces } from 'src/workspaces';
|
||||
|
||||
export class BaseProvider {
|
||||
static id = 'base';
|
||||
protected _apis!: Readonly<Apis>;
|
||||
protected _config!: Readonly<ConfigStore>;
|
||||
public readonly id: string = 'base';
|
||||
protected _workspaces!: Workspaces;
|
||||
protected _logger!: Logger;
|
||||
protected _signals!: DataCenterSignals;
|
||||
protected _workspace!: Workspace;
|
||||
protected _blobs!: BlobStorage;
|
||||
|
||||
constructor() {
|
||||
// Nothing to do here
|
||||
public inject({
|
||||
logger,
|
||||
workspaces,
|
||||
}: {
|
||||
logger: Logger;
|
||||
workspaces: Workspaces;
|
||||
}) {
|
||||
this._logger = logger;
|
||||
this._workspaces = workspaces;
|
||||
}
|
||||
|
||||
get id(): string {
|
||||
return (this.constructor as any).id;
|
||||
/**
|
||||
* hook after provider registered
|
||||
*/
|
||||
public async init() {
|
||||
return;
|
||||
}
|
||||
|
||||
async init(params: InitialParams) {
|
||||
this._apis = params.apis;
|
||||
this._config = params.config;
|
||||
this._logger = params.logger;
|
||||
this._signals = params.signals;
|
||||
this._workspace = params.workspace;
|
||||
this._logger.enabled = params.debug;
|
||||
/**
|
||||
* auth provider
|
||||
*/
|
||||
public async auth() {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* logout provider
|
||||
*/
|
||||
public async logout() {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* warp workspace with workspace functions
|
||||
* @param workspace
|
||||
* @returns
|
||||
*/
|
||||
public async warpWorkspace(workspace: Workspace): Promise<Workspace> {
|
||||
return workspace;
|
||||
}
|
||||
|
||||
/**
|
||||
* load workspaces
|
||||
**/
|
||||
public async loadWorkspaces(): Promise<WS[]> {
|
||||
throw new Error(`provider: ${this.id} loadWorkSpace Not implemented`);
|
||||
}
|
||||
|
||||
/**
|
||||
* get auth user info
|
||||
* @returns
|
||||
*/
|
||||
public async getUserInfo(): Promise<User | undefined> {
|
||||
return;
|
||||
}
|
||||
|
||||
async getBlob(id: string): Promise<string | null> {
|
||||
return this._blobs.get(id);
|
||||
}
|
||||
|
||||
async setBlob(blob: Blob): Promise<string> {
|
||||
return this._blobs.set(blob);
|
||||
}
|
||||
|
||||
/**
|
||||
* clear all local data in provider
|
||||
*/
|
||||
async clear() {
|
||||
await this.destroy();
|
||||
await this._config.clear();
|
||||
this._blobs.clear();
|
||||
}
|
||||
|
||||
async destroy() {
|
||||
// Nothing to do here
|
||||
/**
|
||||
* delete workspace include all data
|
||||
* @param id workspace id
|
||||
*/
|
||||
public async delete(id: string): Promise<void> {
|
||||
id;
|
||||
return;
|
||||
}
|
||||
|
||||
async initData() {
|
||||
throw Error('Not implemented: initData');
|
||||
/**
|
||||
* leave workspace by workspace id
|
||||
* @param id workspace id
|
||||
*/
|
||||
public async leave(id: string): Promise<void> {
|
||||
id;
|
||||
return;
|
||||
}
|
||||
|
||||
// should return a blob url
|
||||
async getBlob(_id: string): Promise<string | null> {
|
||||
throw Error('Not implemented: getBlob');
|
||||
/**
|
||||
* close db link and websocket connection and other resources
|
||||
* @param id workspace id
|
||||
*/
|
||||
public async close(id: string) {
|
||||
id;
|
||||
return;
|
||||
}
|
||||
|
||||
// should return a blob unique id
|
||||
async setBlob(_blob: Blob): Promise<string> {
|
||||
throw Error('Not implemented: setBlob');
|
||||
/**
|
||||
* invite workspace member
|
||||
* @param id workspace id
|
||||
*/
|
||||
public async invite(id: string, email: string): Promise<void> {
|
||||
id;
|
||||
email;
|
||||
return;
|
||||
}
|
||||
|
||||
get workspace() {
|
||||
return this._workspace;
|
||||
/**
|
||||
* remove workspace member by permission id
|
||||
* @param permissionId
|
||||
*/
|
||||
public async removeMember(permissionId: number): Promise<void> {
|
||||
permissionId;
|
||||
return;
|
||||
}
|
||||
|
||||
static async auth(
|
||||
_config: Readonly<ConfigStore>,
|
||||
logger: Logger,
|
||||
_signals: DataCenterSignals
|
||||
) {
|
||||
logger("This provider doesn't require authentication");
|
||||
}
|
||||
|
||||
// get workspace list,return a map of workspace id and boolean
|
||||
// if value is true, it exists locally, otherwise it does not exist locally
|
||||
static async list(
|
||||
_config: Readonly<ConfigStore>
|
||||
): Promise<Map<string, boolean> | undefined> {
|
||||
throw Error('Not implemented: list');
|
||||
public async createWorkspace(
|
||||
meta: WorkspaceMeta
|
||||
): Promise<Workspace | undefined> {
|
||||
meta;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1 @@
|
||||
import type { Workspace } from '@blocksuite/store';
|
||||
|
||||
import type { Apis } from '../apis';
|
||||
import type { DataCenterSignals } from '../datacenter';
|
||||
import type { getLogger } from '../index';
|
||||
import type { ConfigStore } from '../store';
|
||||
|
||||
export type Logger = ReturnType<typeof getLogger>;
|
||||
|
||||
export type InitialParams = {
|
||||
apis: Apis;
|
||||
config: Readonly<ConfigStore>;
|
||||
debug: boolean;
|
||||
logger: Logger;
|
||||
signals: DataCenterSignals;
|
||||
workspace: Workspace;
|
||||
};
|
||||
|
||||
export type { Apis, ConfigStore, DataCenterSignals, Workspace };
|
||||
export type { BaseProvider } from './base.js';
|
||||
export { AffineProvider } from './affine/index.js';
|
||||
export { LocalProvider } from './local/index.js';
|
||||
export * from './affine/affine';
|
||||
|
||||
203
packages/data-center/src/provider/indexeddb.ts
Normal file
203
packages/data-center/src/provider/indexeddb.ts
Normal file
@@ -0,0 +1,203 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import * as idb from 'lib0/indexeddb.js';
|
||||
import { Observable } from 'lib0/observable.js';
|
||||
import type { Doc } from 'yjs';
|
||||
import { applyUpdate, encodeStateAsUpdate, transact } from 'yjs';
|
||||
|
||||
const customStoreName = 'custom';
|
||||
const updatesStoreName = 'updates';
|
||||
|
||||
const PREFERRED_TRIM_SIZE = 500;
|
||||
|
||||
const fetchUpdates = async (provider: IndexedDBProvider) => {
|
||||
const [updatesStore] = idb.transact(provider.db as IDBDatabase, [
|
||||
updatesStoreName,
|
||||
]); // , 'readonly')
|
||||
if (updatesStore) {
|
||||
const updates = await idb.getAll(
|
||||
updatesStore,
|
||||
idb.createIDBKeyRangeLowerBound(provider._dbref, false)
|
||||
);
|
||||
transact(
|
||||
provider.doc,
|
||||
() => {
|
||||
updates.forEach(val => applyUpdate(provider.doc, val));
|
||||
},
|
||||
provider,
|
||||
false
|
||||
);
|
||||
const lastKey = await idb.getLastKey(updatesStore);
|
||||
provider._dbref = lastKey + 1;
|
||||
const cnt = await idb.count(updatesStore);
|
||||
provider._dbsize = cnt;
|
||||
}
|
||||
return updatesStore;
|
||||
};
|
||||
|
||||
const storeState = (provider: IndexedDBProvider, forceStore = true) =>
|
||||
fetchUpdates(provider).then(updatesStore => {
|
||||
if (
|
||||
updatesStore &&
|
||||
(forceStore || provider._dbsize >= PREFERRED_TRIM_SIZE)
|
||||
) {
|
||||
idb
|
||||
.addAutoKey(updatesStore, encodeStateAsUpdate(provider.doc))
|
||||
.then(() =>
|
||||
idb.del(
|
||||
updatesStore,
|
||||
idb.createIDBKeyRangeUpperBound(provider._dbref, true)
|
||||
)
|
||||
)
|
||||
.then(() =>
|
||||
idb.count(updatesStore).then(cnt => {
|
||||
provider._dbsize = cnt;
|
||||
})
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
export class IndexedDBProvider extends Observable<string> {
|
||||
doc: Doc;
|
||||
name: string;
|
||||
_dbref: number;
|
||||
_dbsize: number;
|
||||
private _destroyed: boolean;
|
||||
whenSynced: Promise<IndexedDBProvider>;
|
||||
db: IDBDatabase | null;
|
||||
private _db: Promise<IDBDatabase>;
|
||||
private _storeTimeout: number;
|
||||
private _storeTimeoutId: NodeJS.Timeout | null;
|
||||
private _storeUpdate: (update: Uint8Array, origin: any) => void;
|
||||
|
||||
constructor(name: string, doc: Doc) {
|
||||
super();
|
||||
this.doc = doc;
|
||||
this.name = name;
|
||||
this._dbref = 0;
|
||||
this._dbsize = 0;
|
||||
this._destroyed = false;
|
||||
this.db = null;
|
||||
this._db = idb.openDB(name, db =>
|
||||
idb.createStores(db, [['updates', { autoIncrement: true }], ['custom']])
|
||||
);
|
||||
|
||||
this.whenSynced = this._db.then(async db => {
|
||||
this.db = db;
|
||||
const currState = encodeStateAsUpdate(doc);
|
||||
const updatesStore = await fetchUpdates(this);
|
||||
if (updatesStore) {
|
||||
await idb.addAutoKey(updatesStore, currState);
|
||||
}
|
||||
if (this._destroyed) {
|
||||
return this;
|
||||
}
|
||||
this.emit('synced', [this]);
|
||||
return this;
|
||||
});
|
||||
|
||||
// Timeout in ms untill data is merged and persisted in idb.
|
||||
this._storeTimeout = 1000;
|
||||
|
||||
this._storeTimeoutId = null;
|
||||
|
||||
this._storeUpdate = (update: Uint8Array, origin: any) => {
|
||||
if (this.db && origin !== this) {
|
||||
const [updatesStore] = idb.transact(
|
||||
/** @type {IDBDatabase} */ this.db,
|
||||
[updatesStoreName]
|
||||
);
|
||||
if (updatesStore) {
|
||||
idb.addAutoKey(updatesStore, update);
|
||||
}
|
||||
if (++this._dbsize >= PREFERRED_TRIM_SIZE) {
|
||||
// debounce store call
|
||||
if (this._storeTimeoutId !== null) {
|
||||
clearTimeout(this._storeTimeoutId);
|
||||
}
|
||||
this._storeTimeoutId = setTimeout(() => {
|
||||
storeState(this, false);
|
||||
this._storeTimeoutId = null;
|
||||
}, this._storeTimeout);
|
||||
}
|
||||
}
|
||||
};
|
||||
doc.on('update', this._storeUpdate);
|
||||
this.destroy = this.destroy.bind(this);
|
||||
doc.on('destroy', this.destroy);
|
||||
}
|
||||
|
||||
override destroy() {
|
||||
if (this._storeTimeoutId) {
|
||||
clearTimeout(this._storeTimeoutId);
|
||||
}
|
||||
this.doc.off('update', this._storeUpdate);
|
||||
this.doc.off('destroy', this.destroy);
|
||||
this._destroyed = true;
|
||||
return this._db.then(db => {
|
||||
db.close();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys this instance and removes all data from indexeddb.
|
||||
*
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
async clearData(): Promise<void> {
|
||||
return this.destroy().then(() => {
|
||||
idb.deleteDB(this.name);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String | number | ArrayBuffer | Date} key
|
||||
* @return {Promise<String | number | ArrayBuffer | Date | any>}
|
||||
*/
|
||||
async get(
|
||||
key: string | number | ArrayBuffer | Date
|
||||
): Promise<string | number | ArrayBuffer | Date | any> {
|
||||
return this._db.then(db => {
|
||||
const [custom] = idb.transact(db, [customStoreName], 'readonly');
|
||||
if (custom) {
|
||||
return idb.get(custom, key);
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String | number | ArrayBuffer | Date} key
|
||||
* @param {String | number | ArrayBuffer | Date} value
|
||||
* @return {Promise<String | number | ArrayBuffer | Date>}
|
||||
*/
|
||||
async set(
|
||||
key: string | number | ArrayBuffer | Date,
|
||||
value: string | number | ArrayBuffer | Date
|
||||
): Promise<string | number | ArrayBuffer | Date> {
|
||||
return this._db.then(db => {
|
||||
const [custom] = idb.transact(db, [customStoreName]);
|
||||
if (custom) {
|
||||
return idb.put(custom, value, key);
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String | number | ArrayBuffer | Date} key
|
||||
* @return {Promise<undefined>}
|
||||
*/
|
||||
async del(key: string | number | ArrayBuffer | Date): Promise<undefined> {
|
||||
return this._db.then(db => {
|
||||
const [custom] = idb.transact(db, [customStoreName]);
|
||||
if (custom) {
|
||||
return idb.del(custom, key);
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
}
|
||||
|
||||
static delete(name: string): Promise<void> {
|
||||
return idb.deleteDB(name);
|
||||
}
|
||||
}
|
||||
@@ -1,73 +1 @@
|
||||
import type { BlobStorage } from '@blocksuite/store';
|
||||
import assert from 'assert';
|
||||
|
||||
import type { ConfigStore, InitialParams } from '../index.js';
|
||||
import { BaseProvider } from '../base.js';
|
||||
import { IndexedDBProvider } from './indexeddb.js';
|
||||
|
||||
export class LocalProvider extends BaseProvider {
|
||||
static id = 'local';
|
||||
private _blobs!: BlobStorage;
|
||||
private _idb?: IndexedDBProvider = undefined;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
async init(params: InitialParams) {
|
||||
super.init(params);
|
||||
|
||||
const blobs = await this._workspace.blobs;
|
||||
assert(blobs);
|
||||
this._blobs = blobs;
|
||||
}
|
||||
|
||||
async initData(locally = true) {
|
||||
assert(this._workspace.room);
|
||||
this._logger('Loading local data');
|
||||
this._idb = new IndexedDBProvider(
|
||||
this._workspace.room,
|
||||
this._workspace.doc
|
||||
);
|
||||
|
||||
await this._idb.whenSynced;
|
||||
this._logger('Local data loaded');
|
||||
|
||||
this._signals.listAdd.emit({
|
||||
workspace: this._workspace.room,
|
||||
provider: this.id,
|
||||
locally,
|
||||
});
|
||||
}
|
||||
|
||||
async clear() {
|
||||
assert(this._workspace.room);
|
||||
await super.clear();
|
||||
await this._blobs.clear();
|
||||
await this._idb?.clearData();
|
||||
this._signals.listRemove.emit(this._workspace.room);
|
||||
}
|
||||
|
||||
async destroy(): Promise<void> {
|
||||
super.destroy();
|
||||
await this._idb?.destroy();
|
||||
}
|
||||
|
||||
async getBlob(id: string): Promise<string | null> {
|
||||
return this._blobs.get(id);
|
||||
}
|
||||
|
||||
async setBlob(blob: Blob): Promise<string> {
|
||||
return this._blobs.set(blob);
|
||||
}
|
||||
|
||||
static async list(
|
||||
config: Readonly<ConfigStore<boolean>>
|
||||
): Promise<Map<string, boolean> | undefined> {
|
||||
const entries = await config.entries();
|
||||
return new Map(
|
||||
entries
|
||||
.filter(([key]) => key.startsWith('list:'))
|
||||
.map(([key, value]) => [key.slice(5), value])
|
||||
);
|
||||
}
|
||||
}
|
||||
export * from './local';
|
||||
|
||||
81
packages/data-center/src/provider/local/local.ts
Normal file
81
packages/data-center/src/provider/local/local.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { BaseProvider } from '../base';
|
||||
import { varStorage as storage } from 'lib0/storage';
|
||||
import { Workspace as WS, WorkspaceMeta } from 'src/types';
|
||||
import { Workspace } from '@blocksuite/store';
|
||||
import { IndexedDBProvider } from '../indexeddb';
|
||||
import assert from 'assert';
|
||||
|
||||
const WORKSPACE_KEY = 'workspaces';
|
||||
|
||||
export class LocalProvider extends BaseProvider {
|
||||
public id = 'local';
|
||||
private _idbMap: Map<string, IndexedDBProvider> = new Map();
|
||||
private _workspacesList: WS[] = [];
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this._workspacesList = [];
|
||||
}
|
||||
|
||||
private _storeWorkspaces(workspaces: WS[]) {
|
||||
storage.setItem(WORKSPACE_KEY, JSON.stringify(workspaces));
|
||||
}
|
||||
|
||||
private async _initWorkspaceDb(workspace: Workspace) {
|
||||
assert(workspace.room);
|
||||
let idb = this._idbMap.get(workspace.room);
|
||||
idb?.destroy();
|
||||
idb = new IndexedDBProvider(workspace.room, workspace.doc);
|
||||
this._idbMap.set(workspace.room, idb);
|
||||
return idb;
|
||||
}
|
||||
|
||||
public override async warpWorkspace(
|
||||
workspace: Workspace
|
||||
): Promise<Workspace> {
|
||||
assert(workspace.room);
|
||||
await this._initWorkspaceDb(workspace);
|
||||
return workspace;
|
||||
}
|
||||
|
||||
override loadWorkspaces() {
|
||||
const workspaceStr = storage.getItem(WORKSPACE_KEY);
|
||||
if (workspaceStr) {
|
||||
try {
|
||||
return JSON.parse(workspaceStr);
|
||||
} catch (error) {
|
||||
this._logger(`Failed to parse workspaces from storage`);
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
public override async delete(id: string): Promise<void> {
|
||||
const index = this._workspacesList.findIndex(ws => ws.id === id);
|
||||
if (index !== -1) {
|
||||
// TODO delete workspace all data
|
||||
this._workspacesList.splice(index, 1);
|
||||
this._storeWorkspaces(this._workspacesList);
|
||||
} else {
|
||||
this._logger(`Failed to delete workspace ${id}`);
|
||||
}
|
||||
}
|
||||
|
||||
public override async createWorkspace(
|
||||
meta: WorkspaceMeta
|
||||
): Promise<Workspace | undefined> {
|
||||
assert(meta.name, 'Workspace name is required');
|
||||
meta.avatar ?? (meta.avatar = '');
|
||||
const workspaceInfos = this._workspaces.addLocalWorkspace(meta.name);
|
||||
const workspace = new Workspace({ room: workspaceInfos.id });
|
||||
// TODO: add avatar
|
||||
this._storeWorkspaces([...this._workspacesList, workspaceInfos]);
|
||||
this._initWorkspaceDb(workspace);
|
||||
return workspace;
|
||||
}
|
||||
|
||||
public override async clear(): Promise<void> {
|
||||
// TODO: clear all data
|
||||
this._storeWorkspaces([]);
|
||||
}
|
||||
}
|
||||
175
packages/data-center/src/provider_bk/affine/index.ts
Normal file
175
packages/data-center/src/provider_bk/affine/index.ts
Normal file
@@ -0,0 +1,175 @@
|
||||
import assert from 'assert';
|
||||
import { applyUpdate, Doc } from 'yjs';
|
||||
|
||||
import type {
|
||||
ConfigStore,
|
||||
DataCenterSignals,
|
||||
InitialParams,
|
||||
Logger,
|
||||
} from '../index.js';
|
||||
import { token, Callback, getApis } from '../../apis/index.js';
|
||||
import { LocalProvider } from '../local/index.js';
|
||||
|
||||
import { WebsocketProvider } from './sync.js';
|
||||
import { IndexedDBProvider } from '../local/indexeddb.js';
|
||||
|
||||
export class AffineProvider extends LocalProvider {
|
||||
static id = 'affine';
|
||||
private _onTokenRefresh?: Callback = undefined;
|
||||
private _ws?: WebsocketProvider;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
async init(params: InitialParams) {
|
||||
super.init(params);
|
||||
|
||||
this._onTokenRefresh = () => {
|
||||
if (token.refresh) {
|
||||
this._config.set('token', token.refresh);
|
||||
}
|
||||
};
|
||||
assert(this._onTokenRefresh);
|
||||
|
||||
token.onChange(this._onTokenRefresh);
|
||||
|
||||
// initial login token
|
||||
if (token.isExpired) {
|
||||
try {
|
||||
const refreshToken = await this._config.get('token');
|
||||
await token.refreshToken(refreshToken);
|
||||
|
||||
if (token.refresh) {
|
||||
this._config.set('token', token.refresh);
|
||||
}
|
||||
|
||||
assert(token.isLogin);
|
||||
} catch (_) {
|
||||
this._logger('Authorization failed, fallback to local mode');
|
||||
}
|
||||
} else {
|
||||
this._config.set('token', token.refresh);
|
||||
}
|
||||
}
|
||||
|
||||
async destroy() {
|
||||
if (this._onTokenRefresh) {
|
||||
token.offChange(this._onTokenRefresh);
|
||||
}
|
||||
this._ws?.disconnect();
|
||||
}
|
||||
|
||||
async initData() {
|
||||
const databases = await indexedDB.databases();
|
||||
await super.initData(
|
||||
// set locally to true if exists a same name db
|
||||
databases
|
||||
.map(db => db.name)
|
||||
.filter(v => v)
|
||||
.includes(this._workspace.room)
|
||||
);
|
||||
|
||||
const workspace = this._workspace;
|
||||
const doc = workspace.doc;
|
||||
|
||||
this._logger(`Login: ${token.isLogin}`);
|
||||
|
||||
if (workspace.room && token.isLogin) {
|
||||
try {
|
||||
// init data from cloud
|
||||
await AffineProvider._initCloudDoc(
|
||||
workspace.room,
|
||||
doc,
|
||||
this._logger,
|
||||
this._signals
|
||||
);
|
||||
|
||||
// Wait for ws synchronization to complete, otherwise the data will be modified in reverse, which can be optimized later
|
||||
this._ws = new WebsocketProvider('/', workspace.room, doc);
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
// TODO: synced will also be triggered on reconnection after losing sync
|
||||
// There needs to be an event mechanism to emit the synchronization state to the upper layer
|
||||
assert(this._ws);
|
||||
this._ws.once('synced', () => resolve());
|
||||
this._ws.once('lost-connection', () => resolve());
|
||||
this._ws.once('connection-error', () => reject());
|
||||
});
|
||||
this._signals.listAdd.emit({
|
||||
workspace: workspace.room,
|
||||
provider: this.id,
|
||||
locally: true,
|
||||
});
|
||||
} catch (e) {
|
||||
this._logger('Failed to init cloud workspace', e);
|
||||
}
|
||||
}
|
||||
|
||||
// if after update, the space:meta is empty
|
||||
// then we need to get map with doc
|
||||
// just a workaround for yjs
|
||||
doc.getMap('space:meta');
|
||||
}
|
||||
|
||||
private static async _initCloudDoc(
|
||||
workspace: string,
|
||||
doc: Doc,
|
||||
logger: Logger,
|
||||
signals: DataCenterSignals
|
||||
) {
|
||||
const apis = getApis();
|
||||
logger(`Loading ${workspace}...`);
|
||||
const updates = await apis.downloadWorkspace(workspace);
|
||||
if (updates) {
|
||||
await new Promise(resolve => {
|
||||
doc.once('update', resolve);
|
||||
applyUpdate(doc, new Uint8Array(updates));
|
||||
});
|
||||
logger(`Loaded: ${workspace}`);
|
||||
|
||||
// only add to list as online workspace
|
||||
signals.listAdd.emit({
|
||||
workspace,
|
||||
provider: this.id,
|
||||
// at this time we always download full workspace
|
||||
// but after we support sub doc, we can only download metadata
|
||||
locally: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static async auth(
|
||||
config: Readonly<ConfigStore<string>>,
|
||||
logger: Logger,
|
||||
signals: DataCenterSignals
|
||||
) {
|
||||
const refreshToken = await config.get('token');
|
||||
if (refreshToken) {
|
||||
await token.refreshToken(refreshToken);
|
||||
if (token.isLogin && !token.isExpired) {
|
||||
logger('check login success');
|
||||
// login success
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
logger('start login');
|
||||
// login with google
|
||||
const apis = getApis();
|
||||
assert(apis.signInWithGoogle);
|
||||
const user = await apis.signInWithGoogle();
|
||||
assert(user);
|
||||
logger(`login success: ${user.name}`);
|
||||
|
||||
// TODO: refresh local workspace data
|
||||
const workspaces = await apis.getWorkspaces();
|
||||
await Promise.all(
|
||||
workspaces.map(async ({ id }) => {
|
||||
const doc = new Doc();
|
||||
const idb = new IndexedDBProvider(id, doc);
|
||||
await idb.whenSynced;
|
||||
await this._initCloudDoc(id, doc, logger, signals);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
508
packages/data-center/src/provider_bk/affine/sync.js
Normal file
508
packages/data-center/src/provider_bk/affine/sync.js
Normal file
@@ -0,0 +1,508 @@
|
||||
/* eslint-disable no-undef */
|
||||
/**
|
||||
* @module provider/websocket
|
||||
*/
|
||||
|
||||
/* eslint-env browser */
|
||||
|
||||
// import * as Y from 'yjs'; // eslint-disable-line
|
||||
import * as bc from 'lib0/broadcastchannel';
|
||||
import * as time from 'lib0/time';
|
||||
import * as encoding from 'lib0/encoding';
|
||||
import * as decoding from 'lib0/decoding';
|
||||
import * as syncProtocol from 'y-protocols/sync';
|
||||
import * as authProtocol from 'y-protocols/auth';
|
||||
import * as awarenessProtocol from 'y-protocols/awareness';
|
||||
import { Observable } from 'lib0/observable';
|
||||
import * as math from 'lib0/math';
|
||||
import * as url from 'lib0/url';
|
||||
|
||||
export const messageSync = 0;
|
||||
export const messageQueryAwareness = 3;
|
||||
export const messageAwareness = 1;
|
||||
export const messageAuth = 2;
|
||||
|
||||
/**
|
||||
* encoder, decoder, provider, emitSynced, messageType
|
||||
* @type {Array<function(encoding.Encoder, decoding.Decoder, WebsocketProvider, boolean, number):void>}
|
||||
*/
|
||||
const messageHandlers = [];
|
||||
|
||||
messageHandlers[messageSync] = (
|
||||
encoder,
|
||||
decoder,
|
||||
provider,
|
||||
emitSynced,
|
||||
_messageType
|
||||
) => {
|
||||
encoding.writeVarUint(encoder, messageSync);
|
||||
const syncMessageType = syncProtocol.readSyncMessage(
|
||||
decoder,
|
||||
encoder,
|
||||
provider.doc,
|
||||
provider
|
||||
);
|
||||
if (
|
||||
emitSynced &&
|
||||
syncMessageType === syncProtocol.messageYjsSyncStep2 &&
|
||||
!provider.synced
|
||||
) {
|
||||
provider.synced = true;
|
||||
}
|
||||
};
|
||||
|
||||
messageHandlers[messageQueryAwareness] = (
|
||||
encoder,
|
||||
_decoder,
|
||||
provider,
|
||||
_emitSynced,
|
||||
_messageType
|
||||
) => {
|
||||
encoding.writeVarUint(encoder, messageAwareness);
|
||||
encoding.writeVarUint8Array(
|
||||
encoder,
|
||||
awarenessProtocol.encodeAwarenessUpdate(
|
||||
provider.awareness,
|
||||
Array.from(provider.awareness.getStates().keys())
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
messageHandlers[messageAwareness] = (
|
||||
_encoder,
|
||||
decoder,
|
||||
provider,
|
||||
_emitSynced,
|
||||
_messageType
|
||||
) => {
|
||||
awarenessProtocol.applyAwarenessUpdate(
|
||||
provider.awareness,
|
||||
decoding.readVarUint8Array(decoder),
|
||||
provider
|
||||
);
|
||||
};
|
||||
|
||||
messageHandlers[messageAuth] = (
|
||||
_encoder,
|
||||
decoder,
|
||||
provider,
|
||||
_emitSynced,
|
||||
_messageType
|
||||
) => {
|
||||
authProtocol.readAuthMessage(decoder, provider.doc, (_ydoc, reason) =>
|
||||
permissionDeniedHandler(provider, reason)
|
||||
);
|
||||
};
|
||||
|
||||
// @todo - this should depend on awareness.outdatedTime
|
||||
const messageReconnectTimeout = 30000;
|
||||
|
||||
/**
|
||||
* @param {WebsocketProvider} provider
|
||||
* @param {string} reason
|
||||
*/
|
||||
const permissionDeniedHandler = (provider, reason) =>
|
||||
console.warn(`Permission denied to access ${provider.url}.\n${reason}`);
|
||||
|
||||
/**
|
||||
* @param {WebsocketProvider} provider
|
||||
* @param {Uint8Array} buf
|
||||
* @param {boolean} emitSynced
|
||||
* @return {encoding.Encoder}
|
||||
*/
|
||||
const readMessage = (provider, buf, emitSynced) => {
|
||||
const decoder = decoding.createDecoder(buf);
|
||||
const encoder = encoding.createEncoder();
|
||||
const messageType = decoding.readVarUint(decoder);
|
||||
const messageHandler = provider.messageHandlers[messageType];
|
||||
if (/** @type {any} */ (messageHandler)) {
|
||||
messageHandler(encoder, decoder, provider, emitSynced, messageType);
|
||||
} else {
|
||||
console.error('Unable to compute message');
|
||||
}
|
||||
return encoder;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {WebsocketProvider} provider
|
||||
*/
|
||||
const setupWS = provider => {
|
||||
if (provider.shouldConnect && provider.ws === null) {
|
||||
const websocket = new provider._WS(provider.url);
|
||||
websocket.binaryType = 'arraybuffer';
|
||||
provider.ws = websocket;
|
||||
provider.wsconnecting = true;
|
||||
provider.wsconnected = false;
|
||||
provider.synced = false;
|
||||
|
||||
websocket.onmessage = event => {
|
||||
provider.wsLastMessageReceived = time.getUnixTime();
|
||||
const encoder = readMessage(provider, new Uint8Array(event.data), true);
|
||||
if (encoding.length(encoder) > 1) {
|
||||
websocket.send(encoding.toUint8Array(encoder));
|
||||
}
|
||||
};
|
||||
websocket.onerror = event => {
|
||||
provider.emit('connection-error', [event, provider]);
|
||||
};
|
||||
websocket.onclose = event => {
|
||||
provider.emit('connection-close', [event, provider]);
|
||||
provider.ws = null;
|
||||
provider.wsconnecting = false;
|
||||
if (provider.wsconnected) {
|
||||
provider.wsconnected = false;
|
||||
provider.synced = false;
|
||||
// update awareness (all users except local left)
|
||||
awarenessProtocol.removeAwarenessStates(
|
||||
provider.awareness,
|
||||
Array.from(provider.awareness.getStates().keys()).filter(
|
||||
client => client !== provider.doc.clientID
|
||||
),
|
||||
provider
|
||||
);
|
||||
provider.emit('status', [
|
||||
{
|
||||
status: 'disconnected',
|
||||
},
|
||||
]);
|
||||
} else {
|
||||
provider.wsUnsuccessfulReconnects++;
|
||||
}
|
||||
// Start with no reconnect timeout and increase timeout by
|
||||
// using exponential backoff starting with 100ms
|
||||
setTimeout(
|
||||
setupWS,
|
||||
math.min(
|
||||
math.pow(2, provider.wsUnsuccessfulReconnects) * 100,
|
||||
provider.maxBackoffTime
|
||||
),
|
||||
provider
|
||||
);
|
||||
};
|
||||
websocket.onopen = () => {
|
||||
provider.wsLastMessageReceived = time.getUnixTime();
|
||||
provider.wsconnecting = false;
|
||||
provider.wsconnected = true;
|
||||
provider.wsUnsuccessfulReconnects = 0;
|
||||
provider.emit('status', [
|
||||
{
|
||||
status: 'connected',
|
||||
},
|
||||
]);
|
||||
// always send sync step 1 when connected
|
||||
const encoder = encoding.createEncoder();
|
||||
encoding.writeVarUint(encoder, messageSync);
|
||||
syncProtocol.writeSyncStep1(encoder, provider.doc);
|
||||
websocket.send(encoding.toUint8Array(encoder));
|
||||
// broadcast local awareness state
|
||||
if (provider.awareness.getLocalState() !== null) {
|
||||
const encoderAwarenessState = encoding.createEncoder();
|
||||
encoding.writeVarUint(encoderAwarenessState, messageAwareness);
|
||||
encoding.writeVarUint8Array(
|
||||
encoderAwarenessState,
|
||||
awarenessProtocol.encodeAwarenessUpdate(provider.awareness, [
|
||||
provider.doc.clientID,
|
||||
])
|
||||
);
|
||||
websocket.send(encoding.toUint8Array(encoderAwarenessState));
|
||||
}
|
||||
};
|
||||
|
||||
provider.emit('status', [
|
||||
{
|
||||
status: 'connecting',
|
||||
},
|
||||
]);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {WebsocketProvider} provider
|
||||
* @param {ArrayBuffer} buf
|
||||
*/
|
||||
const broadcastMessage = (provider, buf) => {
|
||||
if (provider.wsconnected) {
|
||||
/** @type {WebSocket} */ (provider.ws).send(buf);
|
||||
}
|
||||
if (provider.bcconnected) {
|
||||
bc.publish(provider.bcChannel, buf, provider);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Websocket Provider for Yjs. Creates a websocket connection to sync the shared document.
|
||||
* The document name is attached to the provided url. I.e. the following example
|
||||
* creates a websocket connection to http://localhost:1234/my-document-name
|
||||
*
|
||||
* @example
|
||||
* import * as Y from 'yjs'
|
||||
* import { WebsocketProvider } from 'y-websocket'
|
||||
* const doc = new Y.Doc()
|
||||
* const provider = new WebsocketProvider('http://localhost:1234', 'my-document-name', doc)
|
||||
*
|
||||
* @extends {Observable<string>}
|
||||
*/
|
||||
export class WebsocketProvider extends Observable {
|
||||
/**
|
||||
* @param {string} serverUrl
|
||||
* @param {string} roomname
|
||||
* @param {Y.Doc} doc
|
||||
* @param {object} [opts]
|
||||
* @param {boolean} [opts.connect]
|
||||
* @param {awarenessProtocol.Awareness} [opts.awareness]
|
||||
* @param {Object<string,string>} [opts.params]
|
||||
* @param {typeof WebSocket} [opts.WebSocketPolyfill] Optionall provide a WebSocket polyfill
|
||||
* @param {number} [opts.resyncInterval] Request server state every `resyncInterval` milliseconds
|
||||
* @param {number} [opts.maxBackoffTime] Maximum amount of time to wait before trying to reconnect (we try to reconnect using exponential backoff)
|
||||
* @param {boolean} [opts.disableBc] Disable cross-tab BroadcastChannel communication
|
||||
*/
|
||||
constructor(
|
||||
serverUrl,
|
||||
roomname,
|
||||
doc,
|
||||
{
|
||||
connect = true,
|
||||
awareness = new awarenessProtocol.Awareness(doc),
|
||||
params = {},
|
||||
WebSocketPolyfill = WebSocket,
|
||||
resyncInterval = -1,
|
||||
maxBackoffTime = 2500,
|
||||
disableBc = false,
|
||||
} = {}
|
||||
) {
|
||||
super();
|
||||
// ensure that url is always ends with /
|
||||
while (serverUrl[serverUrl.length - 1] === '/') {
|
||||
serverUrl = serverUrl.slice(0, serverUrl.length - 1);
|
||||
}
|
||||
const encodedParams = url.encodeQueryParams(params);
|
||||
this.maxBackoffTime = maxBackoffTime;
|
||||
this.bcChannel = serverUrl + '/' + roomname;
|
||||
this.url =
|
||||
serverUrl +
|
||||
'/' +
|
||||
roomname +
|
||||
(encodedParams.length === 0 ? '' : '?' + encodedParams);
|
||||
this.roomname = roomname;
|
||||
this.doc = doc;
|
||||
this._WS = WebSocketPolyfill;
|
||||
this.awareness = awareness;
|
||||
this.wsconnected = false;
|
||||
this.wsconnecting = false;
|
||||
this.bcconnected = false;
|
||||
this.disableBc = disableBc;
|
||||
this.wsUnsuccessfulReconnects = 0;
|
||||
this.messageHandlers = messageHandlers.slice();
|
||||
/**
|
||||
* @type {boolean}
|
||||
*/
|
||||
this._synced = false;
|
||||
/**
|
||||
* @type {WebSocket?}
|
||||
*/
|
||||
this.ws = null;
|
||||
this.wsLastMessageReceived = 0;
|
||||
/**
|
||||
* Whether to connect to other peers or not
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.shouldConnect = connect;
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
this._resyncInterval = 0;
|
||||
if (resyncInterval > 0) {
|
||||
this._resyncInterval = /** @type {any} */ (
|
||||
setInterval(() => {
|
||||
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
||||
// resend sync step 1
|
||||
const encoder = encoding.createEncoder();
|
||||
encoding.writeVarUint(encoder, messageSync);
|
||||
syncProtocol.writeSyncStep1(encoder, doc);
|
||||
this.ws.send(encoding.toUint8Array(encoder));
|
||||
}
|
||||
}, resyncInterval)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ArrayBuffer} data
|
||||
* @param {any} origin
|
||||
*/
|
||||
this._bcSubscriber = (data, origin) => {
|
||||
if (origin !== this) {
|
||||
const encoder = readMessage(this, new Uint8Array(data), false);
|
||||
if (encoding.length(encoder) > 1) {
|
||||
bc.publish(this.bcChannel, encoding.toUint8Array(encoder), this);
|
||||
}
|
||||
}
|
||||
};
|
||||
/**
|
||||
* Listens to Yjs updates and sends them to remote peers (ws and broadcastchannel)
|
||||
* @param {Uint8Array} update
|
||||
* @param {any} origin
|
||||
*/
|
||||
this._updateHandler = (update, origin) => {
|
||||
if (origin !== this) {
|
||||
const encoder = encoding.createEncoder();
|
||||
encoding.writeVarUint(encoder, messageSync);
|
||||
syncProtocol.writeUpdate(encoder, update);
|
||||
broadcastMessage(this, encoding.toUint8Array(encoder));
|
||||
}
|
||||
};
|
||||
this.doc.on('update', this._updateHandler);
|
||||
/**
|
||||
* @param {any} changed
|
||||
* @param {any} _origin
|
||||
*/
|
||||
this._awarenessUpdateHandler = ({ added, updated, removed }, _origin) => {
|
||||
const changedClients = added.concat(updated).concat(removed);
|
||||
const encoder = encoding.createEncoder();
|
||||
encoding.writeVarUint(encoder, messageAwareness);
|
||||
encoding.writeVarUint8Array(
|
||||
encoder,
|
||||
awarenessProtocol.encodeAwarenessUpdate(awareness, changedClients)
|
||||
);
|
||||
broadcastMessage(this, encoding.toUint8Array(encoder));
|
||||
};
|
||||
this._unloadHandler = () => {
|
||||
awarenessProtocol.removeAwarenessStates(
|
||||
this.awareness,
|
||||
[doc.clientID],
|
||||
'window unload'
|
||||
);
|
||||
};
|
||||
if (typeof window !== 'undefined') {
|
||||
window.addEventListener('unload', this._unloadHandler);
|
||||
} else if (typeof process !== 'undefined') {
|
||||
process.on('exit', this._unloadHandler);
|
||||
}
|
||||
awareness.on('update', this._awarenessUpdateHandler);
|
||||
this._checkInterval = /** @type {any} */ (
|
||||
setInterval(() => {
|
||||
if (
|
||||
this.wsconnected &&
|
||||
messageReconnectTimeout <
|
||||
time.getUnixTime() - this.wsLastMessageReceived
|
||||
) {
|
||||
// no message received in a long time - not even your own awareness
|
||||
// updates (which are updated every 15 seconds)
|
||||
/** @type {WebSocket} */ (this.ws).close();
|
||||
}
|
||||
}, messageReconnectTimeout / 10)
|
||||
);
|
||||
if (connect) {
|
||||
this.connect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {boolean}
|
||||
*/
|
||||
get synced() {
|
||||
return this._synced;
|
||||
}
|
||||
|
||||
set synced(state) {
|
||||
if (this._synced !== state) {
|
||||
this._synced = state;
|
||||
this.emit('synced', [state]);
|
||||
this.emit('sync', [state]);
|
||||
}
|
||||
}
|
||||
|
||||
destroy() {
|
||||
if (this._resyncInterval !== 0) {
|
||||
clearInterval(this._resyncInterval);
|
||||
}
|
||||
clearInterval(this._checkInterval);
|
||||
this.disconnect();
|
||||
if (typeof window !== 'undefined') {
|
||||
window.removeEventListener('unload', this._unloadHandler);
|
||||
} else if (typeof process !== 'undefined') {
|
||||
process.off('exit', this._unloadHandler);
|
||||
}
|
||||
this.awareness.off('update', this._awarenessUpdateHandler);
|
||||
this.doc.off('update', this._updateHandler);
|
||||
super.destroy();
|
||||
}
|
||||
|
||||
connectBc() {
|
||||
if (this.disableBc) {
|
||||
return;
|
||||
}
|
||||
if (!this.bcconnected) {
|
||||
bc.subscribe(this.bcChannel, this._bcSubscriber);
|
||||
this.bcconnected = true;
|
||||
}
|
||||
// send sync step1 to bc
|
||||
// write sync step 1
|
||||
const encoderSync = encoding.createEncoder();
|
||||
encoding.writeVarUint(encoderSync, messageSync);
|
||||
syncProtocol.writeSyncStep1(encoderSync, this.doc);
|
||||
bc.publish(this.bcChannel, encoding.toUint8Array(encoderSync), this);
|
||||
// broadcast local state
|
||||
const encoderState = encoding.createEncoder();
|
||||
encoding.writeVarUint(encoderState, messageSync);
|
||||
syncProtocol.writeSyncStep2(encoderState, this.doc);
|
||||
bc.publish(this.bcChannel, encoding.toUint8Array(encoderState), this);
|
||||
// write queryAwareness
|
||||
const encoderAwarenessQuery = encoding.createEncoder();
|
||||
encoding.writeVarUint(encoderAwarenessQuery, messageQueryAwareness);
|
||||
bc.publish(
|
||||
this.bcChannel,
|
||||
encoding.toUint8Array(encoderAwarenessQuery),
|
||||
this
|
||||
);
|
||||
// broadcast local awareness state
|
||||
const encoderAwarenessState = encoding.createEncoder();
|
||||
encoding.writeVarUint(encoderAwarenessState, messageAwareness);
|
||||
encoding.writeVarUint8Array(
|
||||
encoderAwarenessState,
|
||||
awarenessProtocol.encodeAwarenessUpdate(this.awareness, [
|
||||
this.doc.clientID,
|
||||
])
|
||||
);
|
||||
bc.publish(
|
||||
this.bcChannel,
|
||||
encoding.toUint8Array(encoderAwarenessState),
|
||||
this
|
||||
);
|
||||
}
|
||||
|
||||
disconnectBc() {
|
||||
// broadcast message with local awareness state set to null (indicating disconnect)
|
||||
const encoder = encoding.createEncoder();
|
||||
encoding.writeVarUint(encoder, messageAwareness);
|
||||
encoding.writeVarUint8Array(
|
||||
encoder,
|
||||
awarenessProtocol.encodeAwarenessUpdate(
|
||||
this.awareness,
|
||||
[this.doc.clientID],
|
||||
new Map()
|
||||
)
|
||||
);
|
||||
broadcastMessage(this, encoding.toUint8Array(encoder));
|
||||
if (this.bcconnected) {
|
||||
bc.unsubscribe(this.bcChannel, this._bcSubscriber);
|
||||
this.bcconnected = false;
|
||||
}
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this.shouldConnect = false;
|
||||
this.disconnectBc();
|
||||
if (this.ws !== null) {
|
||||
this.ws.close();
|
||||
}
|
||||
}
|
||||
|
||||
connect() {
|
||||
this.shouldConnect = true;
|
||||
if (!this.wsconnected && this.ws === null) {
|
||||
setupWS(this);
|
||||
this.connectBc();
|
||||
}
|
||||
}
|
||||
}
|
||||
79
packages/data-center/src/provider_bk/base.ts
Normal file
79
packages/data-center/src/provider_bk/base.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import type { Workspace } from '@blocksuite/store';
|
||||
|
||||
import type {
|
||||
Apis,
|
||||
DataCenterSignals,
|
||||
Logger,
|
||||
InitialParams,
|
||||
ConfigStore,
|
||||
} from './index';
|
||||
|
||||
export class BaseProvider {
|
||||
static id = 'base';
|
||||
protected _apis!: Readonly<Apis>;
|
||||
protected _config!: Readonly<ConfigStore>;
|
||||
protected _logger!: Logger;
|
||||
protected _signals!: DataCenterSignals;
|
||||
protected _workspace!: Workspace;
|
||||
|
||||
constructor() {
|
||||
// Nothing to do here
|
||||
}
|
||||
|
||||
get id(): string {
|
||||
return (this.constructor as any).id;
|
||||
}
|
||||
|
||||
async init(params: InitialParams) {
|
||||
this._apis = params.apis;
|
||||
this._config = params.config;
|
||||
this._logger = params.logger;
|
||||
this._signals = params.signals;
|
||||
this._workspace = params.workspace;
|
||||
this._logger.enabled = params.debug;
|
||||
}
|
||||
|
||||
async clear() {
|
||||
await this.destroy();
|
||||
await this._config.clear();
|
||||
}
|
||||
|
||||
async destroy() {
|
||||
// Nothing to do here
|
||||
}
|
||||
|
||||
async initData() {
|
||||
throw Error('Not implemented: initData');
|
||||
}
|
||||
|
||||
// should return a blob url
|
||||
async getBlob(_id: string): Promise<string | null> {
|
||||
throw Error('Not implemented: getBlob');
|
||||
}
|
||||
|
||||
// should return a blob unique id
|
||||
async setBlob(_blob: Blob): Promise<string> {
|
||||
throw Error('Not implemented: setBlob');
|
||||
}
|
||||
|
||||
get workspace() {
|
||||
return this._workspace;
|
||||
}
|
||||
|
||||
static async auth(
|
||||
_config: Readonly<ConfigStore>,
|
||||
logger: Logger,
|
||||
_signals: DataCenterSignals
|
||||
) {
|
||||
logger("This provider doesn't require authentication");
|
||||
}
|
||||
|
||||
// get workspace list,return a map of workspace id and boolean
|
||||
// if value is true, it exists locally, otherwise it does not exist locally
|
||||
static async list(
|
||||
_config: Readonly<ConfigStore>
|
||||
): Promise<Map<string, boolean> | undefined> {
|
||||
throw Error('Not implemented: list');
|
||||
}
|
||||
}
|
||||
22
packages/data-center/src/provider_bk/index.ts
Normal file
22
packages/data-center/src/provider_bk/index.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import type { Workspace } from '@blocksuite/store';
|
||||
|
||||
import type { Apis } from '../apis';
|
||||
// import type { DataCenterSignals } from '../datacenter';
|
||||
import type { getLogger } from '../index';
|
||||
import type { ConfigStore } from '../store';
|
||||
|
||||
export type Logger = ReturnType<typeof getLogger>;
|
||||
|
||||
// export type InitialParams = {
|
||||
// apis: Apis;
|
||||
// config: Readonly<ConfigStore>;
|
||||
// debug: boolean;
|
||||
// logger: Logger;
|
||||
// signals: DataCenterSignals;
|
||||
// workspace: Workspace;
|
||||
// };
|
||||
|
||||
// export type { Apis, ConfigStore, DataCenterSignals, Workspace };
|
||||
export type { BaseProvider } from './base.js';
|
||||
export { AffineProvider } from './affine/index.js';
|
||||
export { LocalProvider } from './local/index.js';
|
||||
73
packages/data-center/src/provider_bk/local/index.ts
Normal file
73
packages/data-center/src/provider_bk/local/index.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import type { BlobStorage } from '@blocksuite/store';
|
||||
import assert from 'assert';
|
||||
|
||||
import type { ConfigStore, InitialParams } from '../index.js';
|
||||
import { BaseProvider } from '../base.js';
|
||||
import { IndexedDBProvider } from './indexeddb.js';
|
||||
|
||||
export class LocalProvider extends BaseProvider {
|
||||
static id = 'local';
|
||||
private _blobs!: BlobStorage;
|
||||
private _idb?: IndexedDBProvider = undefined;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
async init(params: InitialParams) {
|
||||
super.init(params);
|
||||
|
||||
const blobs = await this._workspace.blobs;
|
||||
assert(blobs);
|
||||
this._blobs = blobs;
|
||||
}
|
||||
|
||||
async initData(locally = true) {
|
||||
assert(this._workspace.room);
|
||||
this._logger('Loading local data');
|
||||
this._idb = new IndexedDBProvider(
|
||||
this._workspace.room,
|
||||
this._workspace.doc
|
||||
);
|
||||
|
||||
await this._idb.whenSynced;
|
||||
this._logger('Local data loaded');
|
||||
|
||||
this._signals.listAdd.emit({
|
||||
workspace: this._workspace.room,
|
||||
provider: this.id,
|
||||
locally,
|
||||
});
|
||||
}
|
||||
|
||||
async clear() {
|
||||
assert(this._workspace.room);
|
||||
await super.clear();
|
||||
await this._blobs.clear();
|
||||
await this._idb?.clearData();
|
||||
this._signals.listRemove.emit(this._workspace.room);
|
||||
}
|
||||
|
||||
async destroy(): Promise<void> {
|
||||
super.destroy();
|
||||
await this._idb?.destroy();
|
||||
}
|
||||
|
||||
async getBlob(id: string): Promise<string | null> {
|
||||
return this._blobs.get(id);
|
||||
}
|
||||
|
||||
async setBlob(blob: Blob): Promise<string> {
|
||||
return this._blobs.set(blob);
|
||||
}
|
||||
|
||||
static async list(
|
||||
config: Readonly<ConfigStore<boolean>>
|
||||
): Promise<Map<string, boolean> | undefined> {
|
||||
const entries = await config.entries();
|
||||
return new Map(
|
||||
entries
|
||||
.filter(([key]) => key.startsWith('list:'))
|
||||
.map(([key, value]) => [key.slice(5), value])
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,14 @@
|
||||
export enum WorkspaceType {
|
||||
local = 'local',
|
||||
cloud = 'cloud',
|
||||
}
|
||||
import { getLogger } from 'src';
|
||||
|
||||
export type Workspace = {
|
||||
name: string;
|
||||
id: string;
|
||||
isPublish?: boolean;
|
||||
avatar?: string;
|
||||
type: WorkspaceType;
|
||||
owner?: User;
|
||||
isLocal?: boolean;
|
||||
memberCount: number;
|
||||
provider: string;
|
||||
};
|
||||
|
||||
export type User = {
|
||||
@@ -22,3 +19,5 @@ export type User = {
|
||||
};
|
||||
|
||||
export type WorkspaceMeta = Pick<Workspace, 'name' | 'avatar'>;
|
||||
|
||||
export type Logger = ReturnType<typeof getLogger>;
|
||||
@@ -1,113 +1,101 @@
|
||||
import { Workspace } from '@blocksuite/store';
|
||||
import { Workspace as WS } from 'src/types';
|
||||
|
||||
import { Observable } from 'lib0/observable';
|
||||
import { WorkspaceDetail } from 'src/apis/workspace';
|
||||
import { DataCenter } from './../datacenter';
|
||||
import { User, Workspace as Wp, WorkspaceType } from './../style';
|
||||
|
||||
function getProvider(providerList: Record<string, boolean>) {
|
||||
return Object.keys(providerList)[0];
|
||||
}
|
||||
|
||||
function isCloudWorkspace(provider: string) {
|
||||
return provider === 'affine';
|
||||
}
|
||||
import { uuidv4 } from '@blocksuite/store';
|
||||
import { DataCenter } from 'src/dataCenterNew';
|
||||
|
||||
export class Workspaces extends Observable<string> {
|
||||
private _workspaces: Wp[];
|
||||
private readonly workspaceInstances: Map<string, Workspace> = new Map();
|
||||
// cache cloud workspace owner
|
||||
_dc: DataCenter;
|
||||
private _workspaces: WS[];
|
||||
private readonly _dc: DataCenter;
|
||||
|
||||
constructor(dataCenter: DataCenter) {
|
||||
constructor(dc: DataCenter) {
|
||||
super();
|
||||
this._workspaces = [];
|
||||
this.workspaceInstances = new Map();
|
||||
this._dc = dataCenter;
|
||||
this._dc = dc;
|
||||
}
|
||||
|
||||
init() {
|
||||
// IMP: init local providers
|
||||
this._dc.auth('local');
|
||||
// add listener on list change
|
||||
this._dc.signals.listAdd.on(e => {
|
||||
this.refreshWorkspaceList();
|
||||
});
|
||||
this._dc.signals.listRemove.on(e => {
|
||||
this.refreshWorkspaceList();
|
||||
});
|
||||
public init() {
|
||||
this._loadWorkspaces();
|
||||
}
|
||||
|
||||
async refreshWorkspaceList() {
|
||||
const workspaceList = await this._dc.list();
|
||||
|
||||
const workspaceMap = Object.keys(workspaceList).map(([id]) => {
|
||||
return this._dc.load(id).then(w => {
|
||||
return { id, workspace: w, provider: getProvider(workspaceList[id]) };
|
||||
});
|
||||
});
|
||||
|
||||
const workspaces = (await Promise.all(workspaceMap)).map(w => {
|
||||
const { id, workspace, provider } = w;
|
||||
if (workspace && !this.workspaceInstances.has(id)) {
|
||||
this.workspaceInstances.set(id, workspace);
|
||||
}
|
||||
return {
|
||||
id,
|
||||
name: (workspace?.doc?.meta.name as string) || '',
|
||||
avatar: (workspace?.meta.avatar as string) || '',
|
||||
type: isCloudWorkspace(provider)
|
||||
? WorkspaceType.cloud
|
||||
: WorkspaceType.local,
|
||||
isLocal: false,
|
||||
isPublish: false,
|
||||
owner: undefined,
|
||||
memberCount: 1,
|
||||
} as Wp;
|
||||
});
|
||||
const getDetailList = (await Promise.all(workspaceMap)).map(w => {
|
||||
const { id, provider } = w;
|
||||
if (provider === 'workspaces') {
|
||||
return new Promise<{ id: string; detail: WorkspaceDetail | null }>(
|
||||
resolve => {
|
||||
this._dc.apis.getWorkspaceDetail({ id }).then(data => {
|
||||
resolve({ id, detail: data || null });
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
const ownerList = await Promise.all(getDetailList);
|
||||
(await Promise.all(ownerList)).forEach(detail => {
|
||||
if (detail) {
|
||||
const { id, detail: workspaceDetail } = detail;
|
||||
if (workspaceDetail) {
|
||||
const { owner, member_count } = workspaceDetail;
|
||||
const currentWorkspace = workspaces.find(w => w.id === id);
|
||||
if (currentWorkspace) {
|
||||
currentWorkspace.owner = {
|
||||
id: owner.id,
|
||||
name: owner.name,
|
||||
avatar: owner.avatar_url,
|
||||
email: owner.email,
|
||||
};
|
||||
currentWorkspace.memberCount = member_count;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
this._updateWorkspaces(workspaces);
|
||||
}
|
||||
|
||||
getWorkspaces() {
|
||||
get workspaces() {
|
||||
return this._workspaces;
|
||||
}
|
||||
|
||||
_updateWorkspaces(workspaces: Wp[]) {
|
||||
/**
|
||||
* emit when workspaces changed
|
||||
* @param {(workspace: WS[]) => void} cb
|
||||
*/
|
||||
onWorkspacesChange(cb: (workspace: WS[]) => void) {
|
||||
this.on('change', cb);
|
||||
}
|
||||
|
||||
private async _loadWorkspaces() {
|
||||
const providers = this._dc.providers;
|
||||
let workspaces: WS[] = [];
|
||||
providers.forEach(async p => {
|
||||
const pWorkspaces = await p.loadWorkspaces();
|
||||
workspaces = [...workspaces, ...pWorkspaces];
|
||||
this._updateWorkspaces([...workspaces, ...pWorkspaces]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* focus load all workspaces list
|
||||
*/
|
||||
public async refreshWorkspaces() {
|
||||
this._loadWorkspaces();
|
||||
}
|
||||
|
||||
private _updateWorkspaces(workspaces: WS[]) {
|
||||
this._workspaces = workspaces;
|
||||
this.emit('change', this._workspaces);
|
||||
}
|
||||
|
||||
onWorkspaceChange(cb: (workspace: Wp) => void) {
|
||||
this.on('change', cb);
|
||||
private _getDefaultWorkspace(name: string): WS {
|
||||
return {
|
||||
name,
|
||||
id: uuidv4(),
|
||||
isPublish: false,
|
||||
avatar: '',
|
||||
owner: undefined,
|
||||
isLocal: true,
|
||||
memberCount: 1,
|
||||
provider: 'local',
|
||||
};
|
||||
}
|
||||
|
||||
/** add a local workspaces */
|
||||
public addLocalWorkspace(name: string) {
|
||||
const workspace = this._getDefaultWorkspace(name);
|
||||
this._updateWorkspaces([...this._workspaces, workspace]);
|
||||
return workspace;
|
||||
}
|
||||
|
||||
/** delete a workspaces by id */
|
||||
public delete(id: string) {
|
||||
const index = this._workspaces.findIndex(w => w.id === id);
|
||||
if (index >= 0) {
|
||||
this._workspaces.splice(index, 1);
|
||||
this._updateWorkspaces(this._workspaces);
|
||||
}
|
||||
}
|
||||
|
||||
/** get workspace info by id */
|
||||
public getWorkspace(id: string) {
|
||||
return this._workspaces.find(w => w.id === id);
|
||||
}
|
||||
|
||||
/** check if workspace exists */
|
||||
public hasWorkspace(id: string) {
|
||||
return this._workspaces.some(w => w.id === id);
|
||||
}
|
||||
|
||||
public updateWorkspaceMeta(id: string, meta: Partial<WS>) {
|
||||
const index = this._workspaces.findIndex(w => w.id === id);
|
||||
if (index >= 0) {
|
||||
this._workspaces[index] = { ...this._workspaces[index], ...meta };
|
||||
this._updateWorkspaces(this._workspaces);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user