Skip to content

Commit

Permalink
use rpc stub for transactor rpc calls
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolay Chunosov <[email protected]>
  • Loading branch information
Chunosov committed Dec 18, 2024
1 parent 79d56fe commit 7421828
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
36 changes: 20 additions & 16 deletions workers/transactor/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright © 2024 Huly Labs.

import { WorkerEntrypoint } from 'cloudflare:workers'
import { RpcTarget, WorkerEntrypoint } from 'cloudflare:workers'
import { Router, error, html } from 'itty-router'
import { decodeToken } from '@hcengineering/server-token'
import type { Transactor } from './transactor'
Expand Down Expand Up @@ -50,25 +50,29 @@ export default {
}
} satisfies ExportedHandler<Env>

export class TransactorRPC extends WorkerEntrypoint<Env> {
async rpcFindAll (
token: string,
_class: Ref<Class<Doc>>,
query?: DocumentQuery<Doc>,
options?: FindOptions<Doc>
): Promise<any> {
const decodedToken = decodeToken(token, true, this.env.SERVER_SECRET)
const id = this.env.TRANSACTOR.idFromName(decodedToken.workspace.name)
const stub = this.env.TRANSACTOR.get(id)
const result = await (stub as any).rpcFindAll(token, _class, query, options)
return result
class TransactorRpcTarget extends RpcTarget {
constructor (
private readonly token: string,
private readonly workspaceId: string,
private readonly transactor: DurableObjectStub<Transactor>
) {
super()
}

async findAll (_class: Ref<Class<Doc>>, query?: DocumentQuery<Doc>, options?: FindOptions<Doc>): Promise<any> {
return (this.transactor as any).rpcFindAll(this.token, this.workspaceId, _class, query, options)
}

async tx (tx: Tx): Promise<any> {
return (this.transactor as any).rpcTx(this.token, this.workspaceId, tx)
}
}

async rpcTx (token: string, tx: Tx): Promise<any> {
export class TransactorRpc extends WorkerEntrypoint<Env> {
async openRpc (token: string, workspaceId: string): Promise<TransactorRpcTarget> {
const decodedToken = decodeToken(token, true, this.env.SERVER_SECRET)
const id = this.env.TRANSACTOR.idFromName(decodedToken.workspace.name)
const stub = this.env.TRANSACTOR.get(id)
const result: any = await (stub as any).rpcTx(token, tx)
return result
return new TransactorRpcTarget(token, workspaceId, stub)
}
}
3 changes: 2 additions & 1 deletion workers/transactor/src/transactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ export class Transactor extends DurableObject<Env> {

async rpcFindAll (
rawToken: string,
workspaceId: string,
_class: Ref<Class<Doc>>,
query?: DocumentQuery<Doc>,
options?: FindOptions<Doc>
Expand All @@ -348,7 +349,7 @@ export class Transactor extends DurableObject<Env> {
return result
}

async rpcTx (rawToken: string, tx: Tx): Promise<any> {
async rpcTx (rawToken: string, workspaceId: string, tx: Tx): Promise<any> {
let result
const cs = this.createDummyClientSocket()
try {
Expand Down

0 comments on commit 7421828

Please sign in to comment.