@nekzus/liop 2.4.0-alpha.18 → 2.4.0-alpha.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,10 @@
1
1
  import { tokensInputTotal, tokensOutputTotal, tokensSavedTotal, operationDurationMs } from './chunk-PAICJCKJ.js';
2
2
  import { log } from './chunk-K3DGJLUY.js';
3
+ import * as fs from 'fs';
4
+ import * as grpc from '@grpc/grpc-js';
5
+ import path from 'path';
6
+ import { fileURLToPath } from 'url';
7
+ import * as protoLoader from '@grpc/proto-loader';
3
8
 
4
9
  // ../../node_modules/.pnpm/gpt-tokenizer@4.0.0/node_modules/gpt-tokenizer/esm/constants.js
5
10
  var DEFAULT_MERGE_CACHE_SIZE = 1e5;
@@ -206706,7 +206711,174 @@ var TokenTelemetryEngine = class _TokenTelemetryEngine {
206706
206711
  _TokenTelemetryEngine.instance = null;
206707
206712
  }
206708
206713
  };
206714
+ var isTlsEnforced = () => process.env.NODE_ENV === "production" || process.env.LIOP_ENFORCE_TLS === "true";
206715
+ var isTlsWarningSuppressed = (tls) => tls?.insecure === true || tls?.suppressWarning === true || process.env.LIOP_SUPPRESS_TLS_WARNING === "true";
206716
+ var hasWarnedInsecureServer = false;
206717
+ var hasWarnedInsecureChannel = false;
206718
+ function createServerCredentials(tls) {
206719
+ if (tls?.mutualTls && !tls.rootCert && !tls.certManager?.getRootCert()) {
206720
+ throw new Error(
206721
+ "[LIOP-TLS] FATAL: Mutual TLS (mTLS) enabled but no root CA certificate (rootCert) provided to verify client certificates."
206722
+ );
206723
+ }
206724
+ if (tls?.certManager) {
206725
+ try {
206726
+ const certChain = tls.certManager.getCertChain();
206727
+ const privateKey = tls.certManager.getPrivateKey();
206728
+ const rootCert = tls.certManager.getRootCert();
206729
+ if (tls.mutualTls && !rootCert) {
206730
+ throw new Error(
206731
+ "[LIOP-TLS] FATAL: Mutual TLS (mTLS) enabled but CertManager has no root CA certificate loaded."
206732
+ );
206733
+ }
206734
+ return grpc.ServerCredentials.createSsl(
206735
+ rootCert,
206736
+ [{ cert_chain: certChain, private_key: privateKey }],
206737
+ Boolean(tls.mutualTls)
206738
+ );
206739
+ } catch (error) {
206740
+ if (isTlsEnforced()) {
206741
+ throw new Error(
206742
+ `[LIOP-TLS] FATAL: CertManager server credential creation failed in enforced mode: ${error}`
206743
+ );
206744
+ }
206745
+ log.warn(
206746
+ `[LIOP-TLS] CertManager failed, falling back to insecure: ${error}`
206747
+ );
206748
+ return grpc.ServerCredentials.createInsecure();
206749
+ }
206750
+ }
206751
+ if (!tls?.certChain || !tls?.privateKey) {
206752
+ if (isTlsEnforced()) {
206753
+ throw new Error(
206754
+ "[LIOP-TLS] FATAL: TLS certificates required in production or when LIOP_ENFORCE_TLS=true."
206755
+ );
206756
+ }
206757
+ if (!isTlsWarningSuppressed(tls) && !hasWarnedInsecureServer) {
206758
+ hasWarnedInsecureServer = true;
206759
+ log.debug(
206760
+ "[LIOP-TLS] No TLS certificates configured \u2014 using insecure server credentials"
206761
+ );
206762
+ }
206763
+ return grpc.ServerCredentials.createInsecure();
206764
+ }
206765
+ try {
206766
+ const rootCert = tls.rootCert ? fs.readFileSync(tls.rootCert) : null;
206767
+ const certChain = fs.readFileSync(tls.certChain);
206768
+ const privateKey = fs.readFileSync(tls.privateKey);
206769
+ if (tls.mutualTls && !rootCert) {
206770
+ throw new Error(
206771
+ "[LIOP-TLS] FATAL: Mutual TLS (mTLS) enabled but no root CA certificate (rootCert) provided to verify client certificates."
206772
+ );
206773
+ }
206774
+ return grpc.ServerCredentials.createSsl(
206775
+ rootCert,
206776
+ [{ cert_chain: certChain, private_key: privateKey }],
206777
+ Boolean(tls.mutualTls)
206778
+ );
206779
+ } catch (error) {
206780
+ if (isTlsEnforced()) {
206781
+ throw new Error(
206782
+ `[LIOP-TLS] FATAL: Server certificate loading failed in production or enforced mode. Refusing insecure fallback to prevent MITM/eavesdropping: ${error}`
206783
+ );
206784
+ }
206785
+ log.warn(
206786
+ `[LIOP-TLS] Server certificate loading failed, falling back to insecure (dev mode): ${error}`
206787
+ );
206788
+ return grpc.ServerCredentials.createInsecure();
206789
+ }
206790
+ }
206791
+ function createChannelCredentials(tls) {
206792
+ if (tls?.certManager) {
206793
+ try {
206794
+ const rootCert = tls.certManager.getRootCert();
206795
+ if (!rootCert) {
206796
+ throw new Error(
206797
+ "[LIOP-TLS] FATAL: Channel TLS requires a root CA certificate in CertManager."
206798
+ );
206799
+ }
206800
+ const certChain = tls.certManager.getCertChain();
206801
+ const privateKey = tls.certManager.getPrivateKey();
206802
+ return grpc.credentials.createSsl(rootCert, privateKey, certChain);
206803
+ } catch (error) {
206804
+ if (isTlsEnforced()) {
206805
+ throw new Error(
206806
+ `[LIOP-TLS] FATAL: Channel certificate loading failed in enforced mode: ${error}`
206807
+ );
206808
+ }
206809
+ log.warn(
206810
+ `[LIOP-TLS] Channel CertManager failed, falling back to insecure: ${error}`
206811
+ );
206812
+ return grpc.credentials.createInsecure();
206813
+ }
206814
+ }
206815
+ if (!tls?.rootCert) {
206816
+ if (isTlsEnforced()) {
206817
+ throw new Error(
206818
+ "[LIOP-TLS] FATAL: TLS root certificate required in production or when LIOP_ENFORCE_TLS=true."
206819
+ );
206820
+ }
206821
+ if (!isTlsWarningSuppressed(tls) && !hasWarnedInsecureChannel) {
206822
+ hasWarnedInsecureChannel = true;
206823
+ log.debug(
206824
+ "[LIOP-TLS] No TLS root certificate configured \u2014 using insecure channel credentials"
206825
+ );
206826
+ }
206827
+ return grpc.credentials.createInsecure();
206828
+ }
206829
+ try {
206830
+ const rootCert = fs.readFileSync(tls.rootCert);
206831
+ const certChain = tls.certChain ? fs.readFileSync(tls.certChain) : void 0;
206832
+ const privateKey = tls.privateKey ? fs.readFileSync(tls.privateKey) : void 0;
206833
+ return grpc.credentials.createSsl(rootCert, privateKey, certChain);
206834
+ } catch (error) {
206835
+ if (isTlsEnforced()) {
206836
+ throw new Error(
206837
+ `[LIOP-TLS] FATAL: Channel certificate loading failed in production or enforced mode. Refusing insecure fallback to prevent MITM/eavesdropping: ${error}`
206838
+ );
206839
+ }
206840
+ log.warn(
206841
+ `[LIOP-TLS] Channel certificate loading failed, falling back to insecure (dev mode): ${error}`
206842
+ );
206843
+ return grpc.credentials.createInsecure();
206844
+ }
206845
+ }
206846
+
206847
+ // src/rpc/channel-options.ts
206848
+ var GRPC_CHANNEL_OPTIONS = {
206849
+ "grpc.keepalive_time_ms": 3e4,
206850
+ "grpc.keepalive_timeout_ms": 1e4,
206851
+ "grpc.keepalive_permit_without_calls": 1,
206852
+ "grpc.max_send_message_length": -1,
206853
+ "grpc.max_receive_message_length": -1,
206854
+ "grpc.enable_retries": 1
206855
+ };
206856
+ var __filename2 = fileURLToPath(import.meta.url);
206857
+ var __dirname2 = path.dirname(__filename2);
206858
+ var PROD_PATHS = [
206859
+ path.resolve(__dirname2, "./protocol/liop_core.proto"),
206860
+ // Flat dist/ (tsup)
206861
+ path.resolve(__dirname2, "../protocol/liop_core.proto")
206862
+ // dist/rpc/ (tsc)
206863
+ ];
206864
+ var DEV_PROTO_PATH = path.resolve(
206865
+ __dirname2,
206866
+ "../../../../protocol/proto/liop_core.proto"
206867
+ );
206868
+ var PROTO_PATH = PROD_PATHS.find((p) => fs.existsSync(p)) || DEV_PROTO_PATH;
206869
+ if (!fs.existsSync(PROTO_PATH)) {
206870
+ log.error(`[LIOP-Proto] CRITICAL: Proto file not found at ${PROTO_PATH}`);
206871
+ }
206872
+ var packageDefinition = protoLoader.loadSync(PROTO_PATH, {
206873
+ keepCase: true,
206874
+ longs: String,
206875
+ enums: String,
206876
+ defaults: true,
206877
+ oneofs: true
206878
+ });
206879
+ var liopProto = grpc.loadPackageDefinition(packageDefinition);
206880
+ var liopV1 = liopProto.liop.v1;
206709
206881
 
206710
- export { HeuristicTokenEstimator, LiopOTelBridge, RealTokenEstimator, TokenTelemetryEngine, createSyncTokenEstimator, createTokenEstimator };
206711
- //# sourceMappingURL=chunk-H2SNR7PB.js.map
206712
- //# sourceMappingURL=chunk-H2SNR7PB.js.map
206882
+ export { GRPC_CHANNEL_OPTIONS, HeuristicTokenEstimator, LiopOTelBridge, RealTokenEstimator, TokenTelemetryEngine, createChannelCredentials, createServerCredentials, createSyncTokenEstimator, createTokenEstimator, liopV1 };
206883
+ //# sourceMappingURL=chunk-UJFWPRDM.js.map
206884
+ //# sourceMappingURL=chunk-UJFWPRDM.js.map