fix price and modular

This commit is contained in:
2026-02-01 17:38:39 +03:30
parent 55f34aa15d
commit 164542d3e2
801 changed files with 16129 additions and 14478 deletions
+139 -71
View File
@@ -9,7 +9,7 @@ function defaultSerialize(i) {
const defaultDeserialize = defaultSerialize;
const { clearTimeout, setTimeout } = globalThis;
const random = Math.random.bind(Math);
function createBirpc(functions, options) {
function createBirpc($functions, options) {
const {
post,
on,
@@ -22,67 +22,104 @@ function createBirpc(functions, options) {
bind = "rpc",
timeout = DEFAULT_TIMEOUT
} = options;
const rpcPromiseMap = /* @__PURE__ */ new Map();
let _promise;
let closed = false;
const rpc = new Proxy({}, {
get(_, method) {
if (method === "$functions")
return functions;
if (method === "$close")
return close;
if (method === "$rejectPendingCalls") {
return rejectPendingCalls;
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}"`);
const req = { m: method, a: args, t: TYPE_REQUEST };
if (optional)
req.o = true;
const send = async (_req) => post(serialize(_req));
if (event) {
await send(req);
return;
}
if (_promiseInit) {
try {
await _promiseInit;
} finally {
_promiseInit = void 0;
}
if (method === "$closed")
return closed;
if (method === "then" && !eventNames.includes("then") && !("then" in functions))
}
let { promise, resolve, reject } = createPromiseWithResolvers();
const id = nanoid();
req.i = id;
let timeoutId;
async function handler(newReq = req) {
if (timeout >= 0) {
timeoutId = setTimeout(() => {
try {
const handleResult = options.onTimeoutError?.call(rpc, method, args);
if (handleResult !== true)
throw new Error(`[birpc] timeout on calling "${method}"`);
} catch (e) {
reject(e);
}
_rpcPromiseMap.delete(id);
}, timeout);
if (typeof timeoutId === "object")
timeoutId = timeoutId.unref?.();
}
_rpcPromiseMap.set(id, { resolve, reject, timeoutId, method });
await send(newReq);
return promise;
}
try {
if (options.onRequest)
await options.onRequest.call(rpc, req, handler, resolve);
else
await handler();
} catch (e) {
if (options.onGeneralError?.call(rpc, e) !== true)
throw e;
return;
} finally {
clearTimeout(timeoutId);
_rpcPromiseMap.delete(id);
}
return promise;
}
const $call = (method, ...args) => _call(method, args, false);
const $callOptional = (method, ...args) => _call(method, args, false, true);
const $callEvent = (method, ...args) => _call(method, args, true);
const $callRaw = (options2) => _call(options2.method, options2.args, options2.event, options2.optional);
const builtinMethods = {
$call,
$callOptional,
$callEvent,
$callRaw,
$rejectPendingCalls,
get $closed() {
return $closed;
},
get $meta() {
return options.meta;
},
$close,
$functions
};
rpc = new Proxy({}, {
get(_, method) {
if (Object.prototype.hasOwnProperty.call(builtinMethods, method))
return builtinMethods[method];
if (method === "then" && !eventNames.includes("then") && !("then" in $functions))
return void 0;
const sendEvent = (...args) => {
post(serialize({ m: method, a: args, t: TYPE_REQUEST }));
};
const sendEvent = (...args) => _call(method, args, true);
if (eventNames.includes(method)) {
sendEvent.asEvent = sendEvent;
return sendEvent;
}
const sendCall = async (...args) => {
if (closed)
throw new Error(`[birpc] rpc is closed, cannot call "${method}"`);
if (_promise) {
try {
await _promise;
} finally {
_promise = void 0;
}
}
return new Promise((resolve, reject) => {
const id = nanoid();
let timeoutId;
if (timeout >= 0) {
timeoutId = setTimeout(() => {
try {
const handleResult = options.onTimeoutError?.(method, args);
if (handleResult !== true)
throw new Error(`[birpc] timeout on calling "${method}"`);
} catch (e) {
reject(e);
}
rpcPromiseMap.delete(id);
}, timeout);
if (typeof timeoutId === "object")
timeoutId = timeoutId.unref?.();
}
rpcPromiseMap.set(id, { resolve, reject, timeoutId, method });
post(serialize({ m: method, a: args, i: id, t: "q" }));
});
};
const sendCall = (...args) => _call(method, args, false);
sendCall.asEvent = sendEvent;
return sendCall;
}
});
function close(customError) {
closed = true;
rpcPromiseMap.forEach(({ reject, method }) => {
function $close(customError) {
$closed = true;
_rpcPromiseMap.forEach(({ reject, method }) => {
const error = new Error(`[birpc] rpc is closed, cannot call "${method}"`);
if (customError) {
customError.cause ??= error;
@@ -90,18 +127,18 @@ function createBirpc(functions, options) {
}
reject(error);
});
rpcPromiseMap.clear();
_rpcPromiseMap.clear();
off(onMessage);
}
function rejectPendingCalls(handler) {
const entries = Array.from(rpcPromiseMap.values());
function $rejectPendingCalls(handler) {
const entries = Array.from(_rpcPromiseMap.values());
const handlerResults = entries.map(({ method, reject }) => {
if (!handler) {
return reject(new Error(`[birpc]: rejected pending call "${method}".`));
}
return handler({ method, reject });
});
rpcPromiseMap.clear();
_rpcPromiseMap.clear();
return handlerResults;
}
async function onMessage(data, ...extra) {
@@ -109,50 +146,52 @@ function createBirpc(functions, options) {
try {
msg = deserialize(data);
} catch (e) {
if (options.onGeneralError?.(e) !== true)
if (options.onGeneralError?.call(rpc, e) !== true)
throw e;
return;
}
if (msg.t === TYPE_REQUEST) {
const { m: method, a: args } = msg;
const { m: method, a: args, o: optional } = msg;
let result, error;
const fn = resolver ? resolver(method, functions[method]) : functions[method];
let fn = await (resolver ? resolver.call(rpc, method, $functions[method]) : $functions[method]);
if (optional)
fn ||= () => void 0;
if (!fn) {
error = new Error(`[birpc] function "${method}" not found`);
} else {
try {
result = await fn.apply(bind === "rpc" ? rpc : functions, args);
result = await fn.apply(bind === "rpc" ? rpc : $functions, args);
} catch (e) {
error = e;
}
}
if (msg.i) {
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;
}
if (!error) {
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;
} catch (e) {
error = e;
if (options.onGeneralError?.(e, method, args) !== true)
if (options.onGeneralError?.call(rpc, e, method, args) !== true)
throw 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) {
if (options.onGeneralError?.(e, method, args) !== true)
if (options.onGeneralError?.call(rpc, e, method, args) !== true)
throw e;
}
}
} else {
const { i: ack, r: result, e: error } = msg;
const promise = rpcPromiseMap.get(ack);
const promise = _rpcPromiseMap.get(ack);
if (promise) {
clearTimeout(promise.timeoutId);
if (error)
@@ -160,10 +199,10 @@ function createBirpc(functions, options) {
else
promise.resolve(result);
}
rpcPromiseMap.delete(ack);
_rpcPromiseMap.delete(ack);
}
}
_promise = on(onMessage);
_promiseInit = on(onMessage);
return rpc;
}
const cacheMap = /* @__PURE__ */ new WeakMap();
@@ -180,15 +219,35 @@ function cachedMap(items, fn) {
function createBirpcGroup(functions, channels, options = {}) {
const getChannels = () => typeof channels === "function" ? channels() : channels;
const getClients = (channels2 = getChannels()) => cachedMap(channels2, (s) => createBirpc(functions, { ...options, ...s }));
function _boardcast(method, args, event, optional) {
const clients = getClients();
return Promise.all(clients.map((c) => c.$callRaw({ method, args, event, optional })));
}
function $call(method, ...args) {
return _boardcast(method, args, false);
}
function $callOptional(method, ...args) {
return _boardcast(method, args, false, true);
}
function $callEvent(method, ...args) {
return _boardcast(method, args, true);
}
const broadcastBuiltin = {
$call,
$callOptional,
$callEvent
};
const broadcastProxy = new Proxy({}, {
get(_, method) {
if (Object.prototype.hasOwnProperty.call(broadcastBuiltin, method))
return broadcastBuiltin[method];
const client = getClients();
const callbacks = client.map((c) => c[method]);
const sendCall = (...args) => {
return Promise.all(callbacks.map((i) => i(...args)));
};
sendCall.asEvent = (...args) => {
callbacks.map((i) => i.asEvent(...args));
sendCall.asEvent = async (...args) => {
await Promise.all(callbacks.map((i) => i.asEvent(...args)));
};
return sendCall;
}
@@ -213,6 +272,15 @@ function createBirpcGroup(functions, channels, options = {}) {
boardcast: broadcastProxy
};
}
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";
function nanoid(size = 21) {
let id = "";