🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

birpc

Package Overview
Dependencies
Maintainers
2
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

birpc - npm Package Compare versions

Comparing version
2.8.0
to
2.9.0
+14
-10
dist/index.cjs

@@ -28,2 +28,3 @@ 'use strict';

let _promiseInit;
let rpc;
async function _call(method, args, event, optional) {

@@ -55,3 +56,3 @@ if ($closed)

try {
const handleResult = options.onTimeoutError?.(method, args);
const handleResult = options.onTimeoutError?.call(rpc, method, args);
if (handleResult !== true)

@@ -73,7 +74,7 @@ throw new Error(`[birpc] timeout on calling "${method}"`);

if (options.onRequest)
await options.onRequest(req, handler, resolve);
await options.onRequest.call(rpc, req, handler, resolve);
else
await handler();
} catch (e) {
if (options.onGeneralError?.(e) !== true)
if (options.onGeneralError?.call(rpc, e) !== true)
throw e;

@@ -100,6 +101,9 @@ return;

},
get $meta() {
return options.meta;
},
$close,
$functions
};
const rpc = new Proxy({}, {
rpc = new Proxy({}, {
get(_, method) {

@@ -149,3 +153,3 @@ if (Object.prototype.hasOwnProperty.call(builtinMethods, method))

} catch (e) {
if (options.onGeneralError?.(e) !== true)
if (options.onGeneralError?.call(rpc, e) !== true)
throw e;

@@ -157,3 +161,3 @@ return;

let result, error;
let fn = await (resolver ? resolver(method, $functions[method]) : $functions[method]);
let fn = await (resolver ? resolver.call(rpc, method, $functions[method]) : $functions[method]);
if (optional)

@@ -172,5 +176,5 @@ fn ||= () => void 0;

if (error && options.onError)
options.onError(error, method, args);
options.onError.call(rpc, error, method, args);
if (error && options.onFunctionError) {
if (options.onFunctionError(error, method, args) === true)
if (options.onFunctionError.call(rpc, error, method, args) === true)
return;

@@ -184,3 +188,3 @@ }

error = e;
if (options.onGeneralError?.(e, method, args) !== true)
if (options.onGeneralError?.call(rpc, e, method, args) !== true)
throw e;

@@ -192,3 +196,3 @@ }

} catch (e) {
if (options.onGeneralError?.(e, method, args) !== true)
if (options.onGeneralError?.call(rpc, e, method, args) !== true)
throw e;

@@ -195,0 +199,0 @@ }

@@ -5,3 +5,3 @@ type ArgumentsType<T> = T extends (...args: infer A) => any ? A : never;

type Thenable<T> = T | PromiseLike<T>;
type BirpcResolver = (name: string, resolved: (...args: unknown[]) => unknown) => Thenable<((...args: unknown[]) => unknown) | undefined>;
type BirpcResolver<This> = (this: This, name: string, resolved: (...args: unknown[]) => unknown) => Thenable<((...args: any[]) => any) | undefined>;
interface ChannelOptions {

@@ -36,8 +36,12 @@ /**

bind?: 'rpc' | 'functions';
/**
* Custom meta data to attached to the RPC instance's `$meta` property
*/
meta?: any;
}
interface EventOptions<Remote> {
interface EventOptions<RemoteFunctions, LocalFunctions extends object = Record<string, never>> {
/**
* Names of remote functions that do not need response.
*/
eventNames?: (keyof Remote)[];
eventNames?: (keyof RemoteFunctions)[];
/**

@@ -54,3 +58,3 @@ * Maximum timeout for waiting for response, in milliseconds.

*/
resolver?: BirpcResolver;
resolver?: BirpcResolver<BirpcReturn<RemoteFunctions, LocalFunctions>>;
/**

@@ -63,3 +67,3 @@ * Hook triggered before an event is sent to the remote

*/
onRequest?: (req: Request, next: (req?: Request) => Promise<any>, resolve: (res: any) => void) => void | Promise<void>;
onRequest?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, req: Request, next: (req?: Request) => Promise<any>, resolve: (res: any) => void) => void | Promise<void>;
/**

@@ -70,3 +74,3 @@ * Custom error handler

*/
onError?: (error: Error, functionName: string, args: any[]) => boolean | void;
onError?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, error: Error, functionName: string, args: any[]) => boolean | void;
/**

@@ -77,3 +81,3 @@ * Custom error handler for errors occurred in local functions being called

*/
onFunctionError?: (error: Error, functionName: string, args: any[]) => boolean | void;
onFunctionError?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, error: Error, functionName: string, args: any[]) => boolean | void;
/**

@@ -84,3 +88,3 @@ * Custom error handler for errors occurred during serialization or messsaging

*/
onGeneralError?: (error: Error, functionName?: string, args?: any[]) => boolean | void;
onGeneralError?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, error: Error, functionName?: string, args?: any[]) => boolean | void;
/**

@@ -91,5 +95,5 @@ * Custom error handler for timeouts

*/
onTimeoutError?: (functionName: string, args: any[]) => boolean | void;
onTimeoutError?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, functionName: string, args: any[]) => boolean | void;
}
type BirpcOptions<Remote> = EventOptions<Remote> & ChannelOptions;
type BirpcOptions<RemoteFunctions, LocalFunctions extends object = Record<string, never>> = EventOptions<RemoteFunctions, LocalFunctions> & ChannelOptions;
type BirpcFn<T> = PromisifyFn<T> & {

@@ -121,2 +125,6 @@ /**

/**
* Custom meta data attached to the RPC instance
*/
readonly $meta: any;
/**
* Close the RPC connection

@@ -211,6 +219,7 @@ */

declare const setTimeout: typeof globalThis.setTimeout;
declare function createBirpc<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>($functions: LocalFunctions, options: BirpcOptions<RemoteFunctions>): BirpcReturn<RemoteFunctions, LocalFunctions>;
declare function createBirpc<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>($functions: LocalFunctions, options: BirpcOptions<RemoteFunctions, LocalFunctions>): BirpcReturn<RemoteFunctions, LocalFunctions>;
declare function cachedMap<T, R>(items: T[], fn: ((i: T) => R)): R[];
declare function createBirpcGroup<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>(functions: LocalFunctions, channels: ChannelOptions[] | (() => ChannelOptions[]), options?: EventOptions<RemoteFunctions>): BirpcGroup<RemoteFunctions, LocalFunctions>;
declare function createBirpcGroup<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>(functions: LocalFunctions, channels: ChannelOptions[] | (() => ChannelOptions[]), options?: EventOptions<RemoteFunctions, LocalFunctions>): BirpcGroup<RemoteFunctions, LocalFunctions>;
export { type ArgumentsType, type BirpcFn, type BirpcGroup, type BirpcGroupFn, type BirpcGroupReturn, type BirpcGroupReturnBuiltin, type BirpcOptions, type BirpcResolver, type BirpcReturn, type BirpcReturnBuiltin, type ChannelOptions, DEFAULT_TIMEOUT, type EventOptions, type PromisifyFn, type ReturnType, type Thenable, cachedMap, createBirpc, createBirpcGroup };
export { DEFAULT_TIMEOUT, cachedMap, createBirpc, createBirpcGroup };
export type { ArgumentsType, BirpcFn, BirpcGroup, BirpcGroupFn, BirpcGroupReturn, BirpcGroupReturnBuiltin, BirpcOptions, BirpcResolver, BirpcReturn, BirpcReturnBuiltin, ChannelOptions, EventOptions, PromisifyFn, ReturnType, Thenable };

@@ -5,3 +5,3 @@ type ArgumentsType<T> = T extends (...args: infer A) => any ? A : never;

type Thenable<T> = T | PromiseLike<T>;
type BirpcResolver = (name: string, resolved: (...args: unknown[]) => unknown) => Thenable<((...args: unknown[]) => unknown) | undefined>;
type BirpcResolver<This> = (this: This, name: string, resolved: (...args: unknown[]) => unknown) => Thenable<((...args: any[]) => any) | undefined>;
interface ChannelOptions {

@@ -36,8 +36,12 @@ /**

bind?: 'rpc' | 'functions';
/**
* Custom meta data to attached to the RPC instance's `$meta` property
*/
meta?: any;
}
interface EventOptions<Remote> {
interface EventOptions<RemoteFunctions, LocalFunctions extends object = Record<string, never>> {
/**
* Names of remote functions that do not need response.
*/
eventNames?: (keyof Remote)[];
eventNames?: (keyof RemoteFunctions)[];
/**

@@ -54,3 +58,3 @@ * Maximum timeout for waiting for response, in milliseconds.

*/
resolver?: BirpcResolver;
resolver?: BirpcResolver<BirpcReturn<RemoteFunctions, LocalFunctions>>;
/**

@@ -63,3 +67,3 @@ * Hook triggered before an event is sent to the remote

*/
onRequest?: (req: Request, next: (req?: Request) => Promise<any>, resolve: (res: any) => void) => void | Promise<void>;
onRequest?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, req: Request, next: (req?: Request) => Promise<any>, resolve: (res: any) => void) => void | Promise<void>;
/**

@@ -70,3 +74,3 @@ * Custom error handler

*/
onError?: (error: Error, functionName: string, args: any[]) => boolean | void;
onError?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, error: Error, functionName: string, args: any[]) => boolean | void;
/**

@@ -77,3 +81,3 @@ * Custom error handler for errors occurred in local functions being called

*/
onFunctionError?: (error: Error, functionName: string, args: any[]) => boolean | void;
onFunctionError?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, error: Error, functionName: string, args: any[]) => boolean | void;
/**

@@ -84,3 +88,3 @@ * Custom error handler for errors occurred during serialization or messsaging

*/
onGeneralError?: (error: Error, functionName?: string, args?: any[]) => boolean | void;
onGeneralError?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, error: Error, functionName?: string, args?: any[]) => boolean | void;
/**

@@ -91,5 +95,5 @@ * Custom error handler for timeouts

*/
onTimeoutError?: (functionName: string, args: any[]) => boolean | void;
onTimeoutError?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, functionName: string, args: any[]) => boolean | void;
}
type BirpcOptions<Remote> = EventOptions<Remote> & ChannelOptions;
type BirpcOptions<RemoteFunctions, LocalFunctions extends object = Record<string, never>> = EventOptions<RemoteFunctions, LocalFunctions> & ChannelOptions;
type BirpcFn<T> = PromisifyFn<T> & {

@@ -121,2 +125,6 @@ /**

/**
* Custom meta data attached to the RPC instance
*/
readonly $meta: any;
/**
* Close the RPC connection

@@ -211,6 +219,7 @@ */

declare const setTimeout: typeof globalThis.setTimeout;
declare function createBirpc<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>($functions: LocalFunctions, options: BirpcOptions<RemoteFunctions>): BirpcReturn<RemoteFunctions, LocalFunctions>;
declare function createBirpc<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>($functions: LocalFunctions, options: BirpcOptions<RemoteFunctions, LocalFunctions>): BirpcReturn<RemoteFunctions, LocalFunctions>;
declare function cachedMap<T, R>(items: T[], fn: ((i: T) => R)): R[];
declare function createBirpcGroup<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>(functions: LocalFunctions, channels: ChannelOptions[] | (() => ChannelOptions[]), options?: EventOptions<RemoteFunctions>): BirpcGroup<RemoteFunctions, LocalFunctions>;
declare function createBirpcGroup<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>(functions: LocalFunctions, channels: ChannelOptions[] | (() => ChannelOptions[]), options?: EventOptions<RemoteFunctions, LocalFunctions>): BirpcGroup<RemoteFunctions, LocalFunctions>;
export { type ArgumentsType, type BirpcFn, type BirpcGroup, type BirpcGroupFn, type BirpcGroupReturn, type BirpcGroupReturnBuiltin, type BirpcOptions, type BirpcResolver, type BirpcReturn, type BirpcReturnBuiltin, type ChannelOptions, DEFAULT_TIMEOUT, type EventOptions, type PromisifyFn, type ReturnType, type Thenable, cachedMap, createBirpc, createBirpcGroup };
export { DEFAULT_TIMEOUT, cachedMap, createBirpc, createBirpcGroup };
export type { ArgumentsType, BirpcFn, BirpcGroup, BirpcGroupFn, BirpcGroupReturn, BirpcGroupReturnBuiltin, BirpcOptions, BirpcResolver, BirpcReturn, BirpcReturnBuiltin, ChannelOptions, EventOptions, PromisifyFn, ReturnType, Thenable };

@@ -5,3 +5,3 @@ type ArgumentsType<T> = T extends (...args: infer A) => any ? A : never;

type Thenable<T> = T | PromiseLike<T>;
type BirpcResolver = (name: string, resolved: (...args: unknown[]) => unknown) => Thenable<((...args: unknown[]) => unknown) | undefined>;
type BirpcResolver<This> = (this: This, name: string, resolved: (...args: unknown[]) => unknown) => Thenable<((...args: any[]) => any) | undefined>;
interface ChannelOptions {

@@ -36,8 +36,12 @@ /**

bind?: 'rpc' | 'functions';
/**
* Custom meta data to attached to the RPC instance's `$meta` property
*/
meta?: any;
}
interface EventOptions<Remote> {
interface EventOptions<RemoteFunctions, LocalFunctions extends object = Record<string, never>> {
/**
* Names of remote functions that do not need response.
*/
eventNames?: (keyof Remote)[];
eventNames?: (keyof RemoteFunctions)[];
/**

@@ -54,3 +58,3 @@ * Maximum timeout for waiting for response, in milliseconds.

*/
resolver?: BirpcResolver;
resolver?: BirpcResolver<BirpcReturn<RemoteFunctions, LocalFunctions>>;
/**

@@ -63,3 +67,3 @@ * Hook triggered before an event is sent to the remote

*/
onRequest?: (req: Request, next: (req?: Request) => Promise<any>, resolve: (res: any) => void) => void | Promise<void>;
onRequest?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, req: Request, next: (req?: Request) => Promise<any>, resolve: (res: any) => void) => void | Promise<void>;
/**

@@ -70,3 +74,3 @@ * Custom error handler

*/
onError?: (error: Error, functionName: string, args: any[]) => boolean | void;
onError?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, error: Error, functionName: string, args: any[]) => boolean | void;
/**

@@ -77,3 +81,3 @@ * Custom error handler for errors occurred in local functions being called

*/
onFunctionError?: (error: Error, functionName: string, args: any[]) => boolean | void;
onFunctionError?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, error: Error, functionName: string, args: any[]) => boolean | void;
/**

@@ -84,3 +88,3 @@ * Custom error handler for errors occurred during serialization or messsaging

*/
onGeneralError?: (error: Error, functionName?: string, args?: any[]) => boolean | void;
onGeneralError?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, error: Error, functionName?: string, args?: any[]) => boolean | void;
/**

@@ -91,5 +95,5 @@ * Custom error handler for timeouts

*/
onTimeoutError?: (functionName: string, args: any[]) => boolean | void;
onTimeoutError?: (this: BirpcReturn<RemoteFunctions, LocalFunctions>, functionName: string, args: any[]) => boolean | void;
}
type BirpcOptions<Remote> = EventOptions<Remote> & ChannelOptions;
type BirpcOptions<RemoteFunctions, LocalFunctions extends object = Record<string, never>> = EventOptions<RemoteFunctions, LocalFunctions> & ChannelOptions;
type BirpcFn<T> = PromisifyFn<T> & {

@@ -121,2 +125,6 @@ /**

/**
* Custom meta data attached to the RPC instance
*/
readonly $meta: any;
/**
* Close the RPC connection

@@ -211,6 +219,7 @@ */

declare const setTimeout: typeof globalThis.setTimeout;
declare function createBirpc<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>($functions: LocalFunctions, options: BirpcOptions<RemoteFunctions>): BirpcReturn<RemoteFunctions, LocalFunctions>;
declare function createBirpc<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>($functions: LocalFunctions, options: BirpcOptions<RemoteFunctions, LocalFunctions>): BirpcReturn<RemoteFunctions, LocalFunctions>;
declare function cachedMap<T, R>(items: T[], fn: ((i: T) => R)): R[];
declare function createBirpcGroup<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>(functions: LocalFunctions, channels: ChannelOptions[] | (() => ChannelOptions[]), options?: EventOptions<RemoteFunctions>): BirpcGroup<RemoteFunctions, LocalFunctions>;
declare function createBirpcGroup<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>(functions: LocalFunctions, channels: ChannelOptions[] | (() => ChannelOptions[]), options?: EventOptions<RemoteFunctions, LocalFunctions>): BirpcGroup<RemoteFunctions, LocalFunctions>;
export { type ArgumentsType, type BirpcFn, type BirpcGroup, type BirpcGroupFn, type BirpcGroupReturn, type BirpcGroupReturnBuiltin, type BirpcOptions, type BirpcResolver, type BirpcReturn, type BirpcReturnBuiltin, type ChannelOptions, DEFAULT_TIMEOUT, type EventOptions, type PromisifyFn, type ReturnType, type Thenable, cachedMap, createBirpc, createBirpcGroup };
export { DEFAULT_TIMEOUT, cachedMap, createBirpc, createBirpcGroup };
export type { ArgumentsType, BirpcFn, BirpcGroup, BirpcGroupFn, BirpcGroupReturn, BirpcGroupReturnBuiltin, BirpcOptions, BirpcResolver, BirpcReturn, BirpcReturnBuiltin, ChannelOptions, EventOptions, PromisifyFn, ReturnType, Thenable };

@@ -26,2 +26,3 @@ const TYPE_REQUEST = "q";

let _promiseInit;
let rpc;
async function _call(method, args, event, optional) {

@@ -53,3 +54,3 @@ if ($closed)

try {
const handleResult = options.onTimeoutError?.(method, args);
const handleResult = options.onTimeoutError?.call(rpc, method, args);
if (handleResult !== true)

@@ -71,7 +72,7 @@ throw new Error(`[birpc] timeout on calling "${method}"`);

if (options.onRequest)
await options.onRequest(req, handler, resolve);
await options.onRequest.call(rpc, req, handler, resolve);
else
await handler();
} catch (e) {
if (options.onGeneralError?.(e) !== true)
if (options.onGeneralError?.call(rpc, e) !== true)
throw e;

@@ -98,6 +99,9 @@ return;

},
get $meta() {
return options.meta;
},
$close,
$functions
};
const rpc = new Proxy({}, {
rpc = new Proxy({}, {
get(_, method) {

@@ -147,3 +151,3 @@ if (Object.prototype.hasOwnProperty.call(builtinMethods, method))

} catch (e) {
if (options.onGeneralError?.(e) !== true)
if (options.onGeneralError?.call(rpc, e) !== true)
throw e;

@@ -155,3 +159,3 @@ return;

let result, error;
let fn = await (resolver ? resolver(method, $functions[method]) : $functions[method]);
let fn = await (resolver ? resolver.call(rpc, method, $functions[method]) : $functions[method]);
if (optional)

@@ -170,5 +174,5 @@ fn ||= () => void 0;

if (error && options.onError)
options.onError(error, method, args);
options.onError.call(rpc, error, method, args);
if (error && options.onFunctionError) {
if (options.onFunctionError(error, method, args) === true)
if (options.onFunctionError.call(rpc, error, method, args) === true)
return;

@@ -182,3 +186,3 @@ }

error = e;
if (options.onGeneralError?.(e, method, args) !== true)
if (options.onGeneralError?.call(rpc, e, method, args) !== true)
throw e;

@@ -190,3 +194,3 @@ }

} catch (e) {
if (options.onGeneralError?.(e, method, args) !== true)
if (options.onGeneralError?.call(rpc, e, method, args) !== true)
throw e;

@@ -193,0 +197,0 @@ }

{
"name": "birpc",
"type": "module",
"version": "2.8.0",
"version": "2.9.0",
"description": "Message based Two-way remote procedure call",

@@ -36,13 +36,13 @@ "author": "Anthony Fu <anthonyfu117@hotmail.com>",

"devDependencies": {
"@antfu/eslint-config": "^4.11.0",
"@antfu/ni": "^24.3.0",
"@types/node": "^22.13.13",
"@antfu/eslint-config": "^6.3.0",
"@antfu/ni": "^28.0.0",
"@types/node": "^24.10.1",
"@vitest/coverage-v8": "3.0.9",
"bumpp": "^10.1.0",
"eslint": "^9.23.0",
"tsx": "^4.19.3",
"typescript": "^5.8.2",
"unbuild": "^3.5.0",
"vite": "^6.2.3",
"vitest": "^3.0.9"
"bumpp": "^10.3.2",
"eslint": "^9.39.1",
"tsx": "^4.21.0",
"typescript": "^5.9.3",
"unbuild": "^3.6.1",
"vite": "^7.2.6",
"vitest": "^4.0.15"
},

@@ -49,0 +49,0 @@ "scripts": {