Compare commits

...

7 Commits

Author SHA1 Message Date
d5d171b280 feat: update version to 1.1.0 in package.json 2025-12-02 15:49:46 +08:00
0d5661d602 Merge pull request '加入插件及调用上下文' (#1) from dev into main
Reviewed-on: #1
2025-12-02 15:48:35 +08:00
b440c38e73 feat: export getRPCContext from RPCContext 2025-12-02 15:46:07 +08:00
d936af9793 feat: add RPCContext 2025-12-02 15:03:33 +08:00
2fa7b851ff feat: export AbstractRPCPlugin and related types from RPCPlugin 2025-11-28 14:13:02 +08:00
4eac61144f Merge branch 'feature/rpc-plugin' into dev
* feature/rpc-plugin:
  feat: add session management and authentication tests for RPC plugin
  feat: add error reason to RPCError in callRequest method
  feat: enhance onCallRequest method with request handling and hook execution
  feat: add session and request handling to CallIncomingBeforeCtx and CallIncomingCtx interfaces
  feat: enhance callRequest method with options validation and hook execution
  feat: comment out abstract onInit and onDestroy methods in AbstractRPCPlugin class
  feat: update CallOutgoingBeforeCtx and CallOutgoingCtx interfaces to use unknown type for options and result
  feat: add unit tests for RPCPlugin hook execution and error handling
  feat: remove unused HookChainInterruptedError class from RPCPlugin
  feat: add RPCPlugin interface and abstract class with hook context definitions
  feat: add plugin management methods to RPCHandler
  feat: add createDeferrablePromise utility function
2025-11-28 14:12:53 +08:00
3a4a54d37c test: enhance error handling in RPC disconnected test 2025-11-27 22:45:48 +08:00
6 changed files with 88 additions and 5 deletions

View File

@@ -0,0 +1,41 @@
import { getRPCContext, RPCContextKey } from "@/core/RPCContext";
import { RPCHandler, RPCSession } from "@/index"
import { getRandomAvailablePort } from "@/utils/utils";
describe('rpc-context.test', () => {
test('context.session', async () => {
const accessed = new WeakMap<RPCSession, string>();
const provider = {
login(name: string) {
const context = getRPCContext(this);
if (!context) {
throw new Error('context is null');
}
accessed.set(context.session, name);
return name;
},
me() {
const context = getRPCContext(this);
if (!context) {
throw new Error('context is null');
}
return accessed.get(context.session) || 'unknown user';
},
}
const server = new RPCHandler();
const client = new RPCHandler();
server.setProvider(provider);
const port = await getRandomAvailablePort();
await server.listen({ port });
const session = await client.connect({ url: `http://localhost:${port}` });
const api = session.getAPI<typeof provider>();
const clientname = 'clientname';
await expect(api.login(clientname)).resolves.toBe(clientname);
await expect(api.me()).resolves.toBe(clientname);
})
})

View File

@@ -33,8 +33,11 @@ describe('Rpc disconnected test', () => {
errorCode: RPCErrorCode.CONNECTION_DISCONNECTED
})
);
await expect(callPromise).rejects.toBeInstanceOf(RPCError);
await expect(callPromise).rejects
.toHaveProperty('errorCode', RPCErrorCode.CONNECTION_DISCONNECTED);
await expect(callPromise).rejects.toMatchObject(
expect.objectContaining({
constructor: RPCError,
errorCode: RPCErrorCode.CONNECTION_DISCONNECTED
})
);
})
})

View File

@@ -1,6 +1,6 @@
{
"name": "@tonecn/typesrpc",
"version": "1.0.2",
"version": "1.1.0",
"description": "A lightweight, type-safe RPC framework for TypeScript with deep nested API support",
"main": "dist/index.js",
"types": "dist/index.d.ts",

27
src/core/RPCContext.ts Normal file
View File

@@ -0,0 +1,27 @@
import { isObject } from "@/utils/utils";
import { RPCSession } from "./RPCSession";
export interface RPCContext {
session: RPCSession;
}
export const RPCContextFlag = Symbol();
export const RPCContextKey = '__rpc_context';
export class RPCContextConstractor {
public [RPCContextKey] = RPCContextFlag;
constructor(public session: RPCSession) { }
}
export const getRPCContext = (self: unknown): RPCContext | null => {
if (!isObject(self)) {
return null;
}
const rpcContext = self[RPCContextKey];
if (!isObject(rpcContext) || rpcContext[RPCContextKey] !== RPCContextFlag) {
return null;
}
return rpcContext as unknown as RPCContext;
}

View File

@@ -9,6 +9,7 @@ import { makeCallPacket, makeCallResponsePacket, parseCallPacket, parseCallRespo
import { RPCPacket } from "./RPCPacket";
import { EventEmitter } from "@/utils/EventEmitter";
import { createHookRunner } from "./RPCPlugin";
import { RPCContextConstractor, RPCContextKey } from "./RPCContext";
function getProviderFunction(provider: RPCProvider, fnPath: string):
[(...args: any[]) => Promise<any>, object] | null {
@@ -372,7 +373,14 @@ export class RPCSession {
}
try {
const result = await fn.bind(fnThis)(...args);
const result = await fn.bind(new Proxy(fnThis, {
get(target, p, receiver) {
if (p === RPCContextKey) {
return new RPCContextConstractor(instance);
}
return Reflect.get(target, p, receiver);
},
}))(...args);
return makeResponse({
status: 'success',
requestPacket: packet,

View File

@@ -12,6 +12,10 @@ export { SocketConnection } from "./core/SocketConnection";
export { SocketServer } from "./core/SocketServer";
export { EventEmitter } from "./utils/EventEmitter";
export { AbstractRPCPlugin, createHookRunner } from './core/RPCPlugin';
export type { RPCPlugin, RPCPluginHooks, RPCPluginHooksCtx } from './core/RPCPlugin';
export { getRPCContext } from './core/RPCContext';
export { injectSocketClient } from "./core/SocketClient";
export { injectSocketServer } from "./core/SocketServer";
import { injectSocketIOImplements } from "./implements/socket.io";