UNPKG

krpc-js

Version:

KRPC client for NodeJs/Bun

121 lines (117 loc) 4.22 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { RpcClient: () => RpcClient, default: () => src_default }); module.exports = __toCommonJS(src_exports); var import_util = require("util"); // src/client.ts var import_class_validator = require("class-validator"); var import_grpc_js = require("@grpc/grpc-js"); var import_tls = require("tls"); var import_krpc_base = require("krpc-base"); function deserialize_OutputProto(buf) { return import_krpc_base.OutputProto.deserializeBinary(new Uint8Array(buf)); } function serialize_InputProto(arg) { return Buffer.from(arg.serializeBinary()); } var NodeClient = class { pathPrefix_; headers_; exceptionHandler_; timeoutMill_; client_; // {(message?: any, ...optionalParams: any[]) : void}; constructor(client, appPath, headers, exceptionHandler, timeoutMill) { this.client_ = client; this.pathPrefix_ = appPath; this.headers_ = headers; this.exceptionHandler_ = exceptionHandler; this.timeoutMill_ = timeoutMill; } // updateToken(accessToken: string): void { // setToken(this.headers_, accessToken); // } async(method, param, cfg) { const input = new import_krpc_base.InputProto(); const eh = cfg?.exceptionHandler || this.exceptionHandler_; if (param !== void 0 && param !== null) { const errors = (0, import_class_validator.validateSync)(param, { validationError: { target: false } }); if (errors.length > 0) { return Promise.reject(new import_krpc_base.RpcError(import_krpc_base.RpcError.CLIENT_INVALID_ARGUMENT, JSON.stringify(errors))).catch(eh); } input.setUtf8(JSON.stringify(param)); } const options = {}; if (this.timeoutMill_) { const deadline = /* @__PURE__ */ new Date(); deadline.setMilliseconds(deadline.getMilliseconds() + this.timeoutMill_); options.deadline = deadline; } let hd = this.headers_; if (cfg?.headers) { hd = (0, import_krpc_base.mergeHeader)(cfg.headers, this.headers_); } return new Promise((resolve, reject) => { this.client_.makeUnaryRequest( this.pathPrefix_ + method, serialize_InputProto, deserialize_OutputProto, input, import_grpc_js.Metadata.fromHttp2Headers(hd), options, (err, response) => { if (err) { return reject(err); } resolve((0, import_krpc_base.toResult)(response)); } ); }).catch(eh); } }; var clientMap = {}; var RpcClient = class { static create(c, options) { let client = clientMap[c.host]; if (!client) { const url = new URL(c.host); client = new import_grpc_js.Client( url.host, url.protocol == "https:" ? import_grpc_js.ChannelCredentials.createFromSecureContext((0, import_tls.createSecureContext)()) : import_grpc_js.ChannelCredentials.createInsecure(), options ); clientMap[c.host] = client; } const [_, appPath, headers] = (0, import_krpc_base.preproc)(c); return new NodeClient(client, appPath, headers, c.exceptionHandler, c.timeoutMill); } }; // src/index.ts globalThis["TextEncoder"] = globalThis["TextEncoder"] || import_util.TextEncoder; globalThis["TextDecoder"] = globalThis["TextDecoder"] || import_util.TextDecoder; var src_default = RpcClient; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { RpcClient });