This commit is contained in:
2026-03-09 20:51:58 +03:30
parent 4102d91ced
commit a4a8f46fac
5169 changed files with 35173 additions and 437568 deletions
+10 -14
View File
@@ -25,7 +25,6 @@ function createBirpc($functions, options) {
let $closed = false;
const _rpcPromiseMap = /* @__PURE__ */ new Map();
let _promiseInit;
let rpc;
async function _call(method, args, event, optional) {
if ($closed)
throw new Error(`[birpc] rpc is closed, cannot call "${method}"`);
@@ -52,7 +51,7 @@ function createBirpc($functions, options) {
if (timeout >= 0) {
timeoutId = setTimeout(() => {
try {
const handleResult = options.onTimeoutError?.call(rpc, method, args);
const handleResult = options.onTimeoutError?.(method, args);
if (handleResult !== true)
throw new Error(`[birpc] timeout on calling "${method}"`);
} catch (e) {
@@ -69,11 +68,11 @@ function createBirpc($functions, options) {
}
try {
if (options.onRequest)
await options.onRequest.call(rpc, req, handler, resolve);
await options.onRequest(req, handler, resolve);
else
await handler();
} catch (e) {
if (options.onGeneralError?.call(rpc, e) !== true)
if (options.onGeneralError?.(e) !== true)
throw e;
return;
} finally {
@@ -95,13 +94,10 @@ function createBirpc($functions, options) {
get $closed() {
return $closed;
},
get $meta() {
return options.meta;
},
$close,
$functions
};
rpc = new Proxy({}, {
const rpc = new Proxy({}, {
get(_, method) {
if (Object.prototype.hasOwnProperty.call(builtinMethods, method))
return builtinMethods[method];
@@ -146,14 +142,14 @@ function createBirpc($functions, options) {
try {
msg = deserialize(data);
} catch (e) {
if (options.onGeneralError?.call(rpc, e) !== true)
if (options.onGeneralError?.(e) !== true)
throw e;
return;
}
if (msg.t === TYPE_REQUEST) {
const { m: method, a: args, o: optional } = msg;
let result, error;
let fn = await (resolver ? resolver.call(rpc, method, $functions[method]) : $functions[method]);
let fn = await (resolver ? resolver(method, $functions[method]) : $functions[method]);
if (optional)
fn ||= () => void 0;
if (!fn) {
@@ -167,9 +163,9 @@ function createBirpc($functions, options) {
}
if (msg.i) {
if (error && options.onError)
options.onError.call(rpc, error, method, args);
options.onError(error, method, args);
if (error && options.onFunctionError) {
if (options.onFunctionError.call(rpc, error, method, args) === true)
if (options.onFunctionError(error, method, args) === true)
return;
}
if (!error) {
@@ -178,14 +174,14 @@ function createBirpc($functions, options) {
return;
} catch (e) {
error = e;
if (options.onGeneralError?.call(rpc, e, method, args) !== true)
if (options.onGeneralError?.(e, method, args) !== true)
throw e;
}
}
try {
await post(serialize({ t: TYPE_RESPONSE, i: msg.i, e: error }), ...extra);
} catch (e) {
if (options.onGeneralError?.call(rpc, e, method, args) !== true)
if (options.onGeneralError?.(e, method, args) !== true)
throw e;
}
}