@objectstack/service-settings 6.1.1 → 6.3.0

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.
package/dist/index.js CHANGED
@@ -558,17 +558,51 @@ function coerceEnvValue(raw, hint) {
558
558
 
559
559
  // src/in-memory-crypto-provider.ts
560
560
  import { createHash, randomBytes, createCipheriv, createDecipheriv } from "crypto";
561
+ var isWebContainerRuntime = () => {
562
+ const g = globalThis;
563
+ return typeof g !== "undefined" && (Boolean(g.process?.versions?.webcontainer) || Boolean(g.process?.env?.SHELL?.includes?.("jsh")) || Boolean(g.process?.env?.STACKBLITZ));
564
+ };
565
+ var nobleGcmPromise;
566
+ var loadNobleGcm = () => {
567
+ if (!nobleGcmPromise) {
568
+ nobleGcmPromise = (async () => {
569
+ try {
570
+ const mod = await import("@noble/ciphers/aes.js");
571
+ return mod.gcm;
572
+ } catch (err) {
573
+ console.warn(
574
+ `[InMemoryCryptoProvider] WebContainer detected but @noble/ciphers not installed: ${err?.message ?? err}. Falling back to node:crypto (will throw).`
575
+ );
576
+ return void 0;
577
+ }
578
+ })();
579
+ }
580
+ return nobleGcmPromise;
581
+ };
561
582
  var InMemoryCryptoProvider = class {
562
583
  constructor(opts = {}) {
563
584
  this.key = opts.key ?? randomBytes(32);
585
+ this.useNoble = isWebContainerRuntime();
564
586
  }
565
587
  async encrypt(plain, ctx) {
566
588
  const iv = randomBytes(12);
567
- const cipher = createCipheriv("aes-256-gcm", this.key, iv);
568
- cipher.setAAD(Buffer.from(this.aadOf(ctx), "utf8"));
569
- const enc = Buffer.concat([cipher.update(plain, "utf8"), cipher.final()]);
570
- const tag = cipher.getAuthTag();
571
- const blob = Buffer.concat([iv, tag, enc]).toString("base64");
589
+ const aad = Buffer.from(this.aadOf(ctx), "utf8");
590
+ const plainBytes = Buffer.from(plain, "utf8");
591
+ let blob;
592
+ if (this.useNoble) {
593
+ const gcm = await loadNobleGcm();
594
+ if (gcm) {
595
+ const cipher = gcm(this.key, iv, aad);
596
+ const ctWithTag = cipher.encrypt(plainBytes);
597
+ const ct = ctWithTag.subarray(0, ctWithTag.length - 16);
598
+ const tag = ctWithTag.subarray(ctWithTag.length - 16);
599
+ blob = Buffer.concat([iv, Buffer.from(tag), Buffer.from(ct)]).toString("base64");
600
+ } else {
601
+ blob = this.encryptNode(plainBytes, iv, aad);
602
+ }
603
+ } else {
604
+ blob = this.encryptNode(plainBytes, iv, aad);
605
+ }
572
606
  return {
573
607
  id: "sec_" + randomBytes(16).toString("hex"),
574
608
  kmsKeyId: "local:in-memory:v1",
@@ -582,8 +616,18 @@ var InMemoryCryptoProvider = class {
582
616
  const iv = buf.subarray(0, 12);
583
617
  const tag = buf.subarray(12, 28);
584
618
  const data = buf.subarray(28);
619
+ const aad = Buffer.from(this.aadOf(ctx), "utf8");
620
+ if (this.useNoble) {
621
+ const gcm = await loadNobleGcm();
622
+ if (gcm) {
623
+ const cipher = gcm(this.key, iv, aad);
624
+ const ctWithTag = Buffer.concat([data, tag]);
625
+ const out = cipher.decrypt(ctWithTag);
626
+ return Buffer.from(out).toString("utf8");
627
+ }
628
+ }
585
629
  const decipher = createDecipheriv("aes-256-gcm", this.key, iv);
586
- decipher.setAAD(Buffer.from(this.aadOf(ctx), "utf8"));
630
+ decipher.setAAD(aad);
587
631
  decipher.setAuthTag(tag);
588
632
  return Buffer.concat([decipher.update(data), decipher.final()]).toString("utf8");
589
633
  }
@@ -600,6 +644,13 @@ var InMemoryCryptoProvider = class {
600
644
  digest(plain) {
601
645
  return "sha256:" + createHash("sha256").update(plain, "utf8").digest("hex");
602
646
  }
647
+ encryptNode(plainBytes, iv, aad) {
648
+ const cipher = createCipheriv("aes-256-gcm", this.key, iv);
649
+ cipher.setAAD(aad);
650
+ const enc = Buffer.concat([cipher.update(plainBytes), cipher.final()]);
651
+ const tag = cipher.getAuthTag();
652
+ return Buffer.concat([iv, tag, enc]).toString("base64");
653
+ }
603
654
  aadOf(ctx) {
604
655
  return [ctx.namespace, ctx.key].join("|");
605
656
  }