Skip to content

Commit

Permalink
feat: init ios-bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Apr 25, 2024
1 parent 74b7d02 commit 3953103
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 19 deletions.
24 changes: 7 additions & 17 deletions packages/frontend/core/src/modules/cloud/services/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,13 @@ export class AuthService extends Service {
}

async signInPassword(credential: { email: string; password: string }) {
const searchParams = new URLSearchParams();
const redirectUri = new URL(location.href);
if (environment.isDesktop) {
redirectUri.pathname = this.buildRedirectUri('/open-app/signin-redirect');
}
searchParams.set('redirect_uri', redirectUri.toString());

const res = await this.fetchService.fetch(
'/api/auth/sign-in?' + searchParams.toString(),
{
method: 'POST',
body: JSON.stringify(credential),
headers: {
'content-type': 'application/json',
},
}
);
const res = await this.fetchService.fetch('/api/auth/sign-in', {
method: 'POST',
body: JSON.stringify(credential),
headers: {
'content-type': 'application/json',
},
});
if (!res.ok) {
throw new Error('Failed to sign in');
}
Expand Down
23 changes: 23 additions & 0 deletions packages/frontend/ios-bridge/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@affine/ios-bridge",
"version": "0.0.0",
"description": "AFFiNE API For iOS app",
"private": true,
"browser": "src/index.ts",
"scripts": {
"build": "yarn workspace @affine/cli build",
"dev": "yarn workspace @affine/cli dev",
"static-server": "yarn workspace @affine/cli dev --static"
},
"dependencies": {
"@affine/core": "workspace:*",
"@affine/env": "workspace:*",
"@sentry/react": "^7.109.0",
"core-js": "^3.36.1",
"intl-segmenter-polyfill-rs": "^0.1.7"
},
"devDependencies": {
"@affine/cli": "workspace:*",
"typescript": "^5.4.5"
}
}
52 changes: 52 additions & 0 deletions packages/frontend/ios-bridge/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import './polyfill/dispose';
import './polyfill/intl-segmenter';
import './polyfill/request-idle-callback';
import '@affine/core/bootstrap/preload';

import { configureCommonModules, configureImpls } from '@affine/core/modules';
import { AuthService } from '@affine/core/modules/cloud';
import {
configureBrowserWorkspaceFlavours,
configureIndexedDBWorkspaceEngineStorageProvider,
} from '@affine/core/modules/workspace-engine';
import {
DocsService,
Framework,
LifecycleService,
type WorkspaceMetadata,
WorkspacesService,
} from '@toeverything/infra';

const framework = new Framework();
configureCommonModules(framework);
configureImpls(framework);
configureBrowserWorkspaceFlavours(framework);
configureIndexedDBWorkspaceEngineStorageProvider(framework);
const frameworkProvider = framework.provider();

// start the application
frameworkProvider.get(LifecycleService).applicationStart();

const jsb = {
signInPassword(email: string, password: string) {
return frameworkProvider
.get(AuthService)
.signInPassword({ email, password });
},

getWorkspacesList() {
return frameworkProvider.get(WorkspacesService).list.workspaces$;
},

getWorkspacesDocs(workspaceMeta: WorkspaceMetadata) {
return frameworkProvider
.get(WorkspacesService)
.open({ metadata: workspaceMeta })
.workspace.scope.get(DocsService)
.list.docs$.map(docs => docs.map(d => d.meta$.value));
},
// ... add more methods here
};

(window as any).jsb = jsb;
(window as any).workspacesService = frameworkProvider.get(WorkspacesService);
2 changes: 2 additions & 0 deletions packages/frontend/ios-bridge/src/polyfill/dispose.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import 'core-js/modules/esnext.symbol.async-dispose';
import 'core-js/modules/esnext.symbol.dispose';
11 changes: 11 additions & 0 deletions packages/frontend/ios-bridge/src/polyfill/intl-segmenter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
if (Intl.Segmenter === undefined) {
await import('intl-segmenter-polyfill-rs').then(({ Segmenter }) => {
Object.defineProperty(Intl, 'Segmenter', {
value: Segmenter,
configurable: true,
writable: true,
});
});
}

export {};
19 changes: 19 additions & 0 deletions packages/frontend/ios-bridge/src/polyfill/request-idle-callback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
window.requestIdleCallback =
window.requestIdleCallback ||
function (cb) {
const start = Date.now();
return setTimeout(function () {
cb({
didTimeout: false,
timeRemaining: function () {
return Math.max(0, 50 - (Date.now() - start));
},
});
}, 1);
};

window.cancelIdleCallback =
window.cancelIdleCallback ||
function (id) {
clearTimeout(id);
};
12 changes: 12 additions & 0 deletions packages/frontend/ios-bridge/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"composite": true,
"outDir": "lib",
"moduleResolution": "Bundler",
"types": ["affine__env"],
"rootDir": "./src"
},
"include": ["./src"],
"references": [{ "path": "../core" }]
}
17 changes: 16 additions & 1 deletion tools/cli/src/bin/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const buildFlags = process.argv.includes('--static')
{
value: 'desktop',
},
{
value: 'ios-bridge',
},
],
initialValue: 'browser',
}),
Expand Down Expand Up @@ -106,12 +109,24 @@ flags.entry = undefined;
const cwd =
flags.distribution === 'browser'
? join(projectRoot, 'packages', 'frontend', 'web')
: join(projectRoot, 'packages', 'frontend', 'electron');
: flags.distribution === 'desktop'
? join(projectRoot, 'packages', 'frontend', 'electron')
: flags.distribution === 'ios-bridge'
? join(projectRoot, 'packages', 'frontend', 'ios-bridge')
: null;

if (!cwd) {
throw new Error('Invalid distribution');
}

if (flags.distribution === 'desktop') {
flags.entry = join(cwd, 'renderer', 'index.tsx');
}

if (flags.distribution === 'ios-bridge') {
flags.entry = join(cwd, 'src', 'index.ts');
}

if (buildFlags.debugBlockSuite) {
const { config } = await import('dotenv');
const envLocal = config({
Expand Down
2 changes: 1 addition & 1 deletion tools/cli/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fileURLToPath } from 'node:url';

export type BuildFlags = {
distribution: 'browser' | 'desktop';
distribution: 'browser' | 'desktop' | 'ios-bridge';
mode: 'development' | 'production';
channel: 'stable' | 'beta' | 'canary' | 'internal';
coverage?: boolean;
Expand Down
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,20 @@ __metadata:
languageName: unknown
linkType: soft

"@affine/ios-bridge@workspace:packages/frontend/ios-bridge":
version: 0.0.0-use.local
resolution: "@affine/ios-bridge@workspace:packages/frontend/ios-bridge"
dependencies:
"@affine/cli": "workspace:*"
"@affine/core": "workspace:*"
"@affine/env": "workspace:*"
"@sentry/react": "npm:^7.109.0"
core-js: "npm:^3.36.1"
intl-segmenter-polyfill-rs: "npm:^0.1.7"
typescript: "npm:^5.4.5"
languageName: unknown
linkType: soft

"@affine/monorepo@workspace:.":
version: 0.0.0-use.local
resolution: "@affine/monorepo@workspace:."
Expand Down

0 comments on commit 3953103

Please sign in to comment.