🚀 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.6.1
to
2.7.0
+41
-16
dist/index.cjs

@@ -26,3 +26,3 @@ 'use strict';

const rpcPromiseMap = /* @__PURE__ */ new Map();
let _promise;
let _promiseInit;
let closed = false;

@@ -42,4 +42,4 @@ const rpc = new Proxy({}, {

return void 0;
const sendEvent = (...args) => {
post(serialize({ m: method, a: args, t: TYPE_REQUEST }));
const sendEvent = async (...args) => {
await post(serialize({ m: method, a: args, t: TYPE_REQUEST }));
};

@@ -53,12 +53,14 @@ if (eventNames.includes(method)) {

throw new Error(`[birpc] rpc is closed, cannot call "${method}"`);
if (_promise) {
if (_promiseInit) {
try {
await _promise;
await _promiseInit;
} finally {
_promise = void 0;
_promiseInit = void 0;
}
}
return new Promise((resolve, reject) => {
const id = nanoid();
let timeoutId;
let { promise, resolve, reject } = createPromiseWithResolvers();
const id = nanoid();
let timeoutId;
const _req = { m: method, a: args, i: id, t: TYPE_REQUEST };
async function handler(req = _req) {
if (timeout >= 0) {

@@ -79,4 +81,18 @@ timeoutId = setTimeout(() => {

rpcPromiseMap.set(id, { resolve, reject, timeoutId, method });
post(serialize({ m: method, a: args, i: id, t: "q" }));
});
await post(serialize(req));
return promise;
}
try {
if (options.onRequest)
await options.onRequest(_req, handler, resolve);
else
await handler();
} catch (e) {
clearTimeout(timeoutId);
rpcPromiseMap.delete(id);
if (options.onGeneralError?.(e) !== true)
throw e;
return;
}
return promise;
};

@@ -142,3 +158,3 @@ sendCall.asEvent = sendEvent;

try {
post(serialize({ t: TYPE_RESPONSE, i: msg.i, r: result }), ...extra);
await post(serialize({ t: TYPE_RESPONSE, i: msg.i, r: result }), ...extra);
return;

@@ -152,3 +168,3 @@ } catch (e) {

try {
post(serialize({ t: TYPE_RESPONSE, i: msg.i, e: error }), ...extra);
await post(serialize({ t: TYPE_RESPONSE, i: msg.i, e: error }), ...extra);
} catch (e) {

@@ -172,3 +188,3 @@ if (options.onGeneralError?.(e, method, args) !== true)

}
_promise = on(onMessage);
_promiseInit = on(onMessage);
return rpc;

@@ -197,4 +213,4 @@ }

};
sendCall.asEvent = (...args) => {
callbacks.map((i) => i.asEvent(...args));
sendCall.asEvent = async (...args) => {
await Promise.all(callbacks.map((i) => i.asEvent(...args)));
};

@@ -224,2 +240,11 @@ return sendCall;

}
function createPromiseWithResolvers() {
let resolve;
let reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return { promise, resolve, reject };
}
const urlAlphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";

@@ -226,0 +251,0 @@ function nanoid(size = 21) {

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

/**
* Hook triggered before an event is sent to the remote
*
* @param req - Request parameters
* @param next - Function to continue the request
* @param resolve - Function to resolve the response directly
*/
onRequest?: (req: Request, next: (req?: Request) => Promise<any>, resolve: (res: any) => void) => void | Promise<void>;
/**
* Custom error handler

@@ -84,3 +92,3 @@ *

*/
asEvent: (...args: ArgumentsType<T>) => void;
asEvent: (...args: ArgumentsType<T>) => Promise<void>;
};

@@ -95,3 +103,3 @@ interface BirpcGroupFn<T> {

*/
asEvent: (...args: ArgumentsType<T>) => void;
asEvent: (...args: ArgumentsType<T>) => Promise<void>;
}

@@ -122,2 +130,21 @@ type BirpcReturn<RemoteFunctions, LocalFunctions = Record<string, never>> = {

}
declare const TYPE_REQUEST: "q";
interface Request {
/**
* Type
*/
t: typeof TYPE_REQUEST;
/**
* ID
*/
i?: string;
/**
* Method
*/
m: string;
/**
* Arguments
*/
a: any[];
}
declare const DEFAULT_TIMEOUT = 60000;

@@ -124,0 +151,0 @@ declare const setTimeout: typeof globalThis.setTimeout;

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

/**
* Hook triggered before an event is sent to the remote
*
* @param req - Request parameters
* @param next - Function to continue the request
* @param resolve - Function to resolve the response directly
*/
onRequest?: (req: Request, next: (req?: Request) => Promise<any>, resolve: (res: any) => void) => void | Promise<void>;
/**
* Custom error handler

@@ -84,3 +92,3 @@ *

*/
asEvent: (...args: ArgumentsType<T>) => void;
asEvent: (...args: ArgumentsType<T>) => Promise<void>;
};

@@ -95,3 +103,3 @@ interface BirpcGroupFn<T> {

*/
asEvent: (...args: ArgumentsType<T>) => void;
asEvent: (...args: ArgumentsType<T>) => Promise<void>;
}

@@ -122,2 +130,21 @@ type BirpcReturn<RemoteFunctions, LocalFunctions = Record<string, never>> = {

}
declare const TYPE_REQUEST: "q";
interface Request {
/**
* Type
*/
t: typeof TYPE_REQUEST;
/**
* ID
*/
i?: string;
/**
* Method
*/
m: string;
/**
* Arguments
*/
a: any[];
}
declare const DEFAULT_TIMEOUT = 60000;

@@ -124,0 +151,0 @@ declare const setTimeout: typeof globalThis.setTimeout;

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

/**
* Hook triggered before an event is sent to the remote
*
* @param req - Request parameters
* @param next - Function to continue the request
* @param resolve - Function to resolve the response directly
*/
onRequest?: (req: Request, next: (req?: Request) => Promise<any>, resolve: (res: any) => void) => void | Promise<void>;
/**
* Custom error handler

@@ -84,3 +92,3 @@ *

*/
asEvent: (...args: ArgumentsType<T>) => void;
asEvent: (...args: ArgumentsType<T>) => Promise<void>;
};

@@ -95,3 +103,3 @@ interface BirpcGroupFn<T> {

*/
asEvent: (...args: ArgumentsType<T>) => void;
asEvent: (...args: ArgumentsType<T>) => Promise<void>;
}

@@ -122,2 +130,21 @@ type BirpcReturn<RemoteFunctions, LocalFunctions = Record<string, never>> = {

}
declare const TYPE_REQUEST: "q";
interface Request {
/**
* Type
*/
t: typeof TYPE_REQUEST;
/**
* ID
*/
i?: string;
/**
* Method
*/
m: string;
/**
* Arguments
*/
a: any[];
}
declare const DEFAULT_TIMEOUT = 60000;

@@ -124,0 +151,0 @@ declare const setTimeout: typeof globalThis.setTimeout;

@@ -24,3 +24,3 @@ const TYPE_REQUEST = "q";

const rpcPromiseMap = /* @__PURE__ */ new Map();
let _promise;
let _promiseInit;
let closed = false;

@@ -40,4 +40,4 @@ const rpc = new Proxy({}, {

return void 0;
const sendEvent = (...args) => {
post(serialize({ m: method, a: args, t: TYPE_REQUEST }));
const sendEvent = async (...args) => {
await post(serialize({ m: method, a: args, t: TYPE_REQUEST }));
};

@@ -51,12 +51,14 @@ if (eventNames.includes(method)) {

throw new Error(`[birpc] rpc is closed, cannot call "${method}"`);
if (_promise) {
if (_promiseInit) {
try {
await _promise;
await _promiseInit;
} finally {
_promise = void 0;
_promiseInit = void 0;
}
}
return new Promise((resolve, reject) => {
const id = nanoid();
let timeoutId;
let { promise, resolve, reject } = createPromiseWithResolvers();
const id = nanoid();
let timeoutId;
const _req = { m: method, a: args, i: id, t: TYPE_REQUEST };
async function handler(req = _req) {
if (timeout >= 0) {

@@ -77,4 +79,18 @@ timeoutId = setTimeout(() => {

rpcPromiseMap.set(id, { resolve, reject, timeoutId, method });
post(serialize({ m: method, a: args, i: id, t: "q" }));
});
await post(serialize(req));
return promise;
}
try {
if (options.onRequest)
await options.onRequest(_req, handler, resolve);
else
await handler();
} catch (e) {
clearTimeout(timeoutId);
rpcPromiseMap.delete(id);
if (options.onGeneralError?.(e) !== true)
throw e;
return;
}
return promise;
};

@@ -140,3 +156,3 @@ sendCall.asEvent = sendEvent;

try {
post(serialize({ t: TYPE_RESPONSE, i: msg.i, r: result }), ...extra);
await post(serialize({ t: TYPE_RESPONSE, i: msg.i, r: result }), ...extra);
return;

@@ -150,3 +166,3 @@ } catch (e) {

try {
post(serialize({ t: TYPE_RESPONSE, i: msg.i, e: error }), ...extra);
await post(serialize({ t: TYPE_RESPONSE, i: msg.i, e: error }), ...extra);
} catch (e) {

@@ -170,3 +186,3 @@ if (options.onGeneralError?.(e, method, args) !== true)

}
_promise = on(onMessage);
_promiseInit = on(onMessage);
return rpc;

@@ -195,4 +211,4 @@ }

};
sendCall.asEvent = (...args) => {
callbacks.map((i) => i.asEvent(...args));
sendCall.asEvent = async (...args) => {
await Promise.all(callbacks.map((i) => i.asEvent(...args)));
};

@@ -222,2 +238,11 @@ return sendCall;

}
function createPromiseWithResolvers() {
let resolve;
let reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return { promise, resolve, reject };
}
const urlAlphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";

@@ -224,0 +249,0 @@ function nanoid(size = 21) {

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

@@ -6,0 +6,0 @@ "author": "Anthony Fu <anthonyfu117@hotmail.com>",