@katlux/providers 0.1.1-alpha.120 → 0.1.1-alpha.121

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.cjs CHANGED
@@ -816,7 +816,31 @@ class AEncryptionProvider {
816
816
  return this.encryptValue(data, options);
817
817
  }
818
818
  if (Array.isArray(data)) {
819
- if (options?.fields && options.fields.length > 0 || options?.valueEncryption === true) {
819
+ if (data.length === 0) return data;
820
+ if (options?.fields && options.fields.length > 0) {
821
+ const fields = options.fields;
822
+ let hasUnencrypted = false;
823
+ for (let i = 0; i < data.length; i++) {
824
+ const item = data[i];
825
+ if (item && typeof item === "object") {
826
+ for (let f = 0; f < fields.length; f++) {
827
+ const val = item[fields[f]];
828
+ if (val !== void 0 && val !== null && !this.isEncrypted(val)) {
829
+ hasUnencrypted = true;
830
+ break;
831
+ }
832
+ }
833
+ if (hasUnencrypted) break;
834
+ }
835
+ }
836
+ if (!hasUnencrypted) {
837
+ return data;
838
+ }
839
+ await this.warmupKey(options);
840
+ const itemOptions = { ...options, _warmed: true };
841
+ return Promise.all(data.map((item) => this.encrypt(item, itemOptions)));
842
+ }
843
+ if (options?.valueEncryption === true) {
820
844
  await this.warmupKey(options);
821
845
  const itemOptions = { ...options, _warmed: true };
822
846
  return Promise.all(data.map((item) => this.encrypt(item, itemOptions)));
@@ -829,7 +853,7 @@ class AEncryptionProvider {
829
853
  (field) => Object.prototype.hasOwnProperty.call(data, field) && data[field] !== void 0 && data[field] !== null && !this.isEncrypted(data[field])
830
854
  );
831
855
  if (fieldsToEncrypt.length === 0) {
832
- return { ...data };
856
+ return data;
833
857
  }
834
858
  if (!options?._warmed) {
835
859
  await this.warmupKey(options);
@@ -887,12 +911,13 @@ class AEncryptionProvider {
887
911
  return Promise.all(payload.map((item) => this.decrypt(item, options)));
888
912
  }
889
913
  if (typeof payload === "object" && !ArrayBuffer.isView(payload) && !(payload instanceof ArrayBuffer)) {
890
- const result = { ...payload };
891
- const targetKeys = options?.fields && options.fields.length > 0 ? options.fields.filter((f) => Object.prototype.hasOwnProperty.call(result, f)) : Object.keys(result);
892
- const keysToDecrypt = targetKeys.filter((key) => this.isEncrypted(result[key]));
914
+ const targetKeys = options?.fields && options.fields.length > 0 ? options.fields.filter((f) => Object.prototype.hasOwnProperty.call(payload, f)) : Object.keys(payload);
915
+ const keysToDecrypt = targetKeys.filter((key) => this.isEncrypted(payload[key]));
893
916
  if (keysToDecrypt.length === 0) {
894
917
  return payload;
895
918
  }
919
+ const proto = Object.getPrototypeOf(payload);
920
+ const result = proto && proto !== Object.prototype ? Object.assign(Object.create(proto), payload) : { ...payload };
896
921
  const entries = await Promise.all(
897
922
  keysToDecrypt.map(async (key) => {
898
923
  const boundOptions = this._bindFieldAad(payload, key, options);
@@ -1758,16 +1783,15 @@ class CWebCryptoEncryptionProvider extends AEncryptionProvider {
1758
1783
  };
1759
1784
  const aad = this._resolveEffectiveAad(options, true);
1760
1785
  let hasAad = false;
1761
- if (payload.aad || aad) {
1762
- if (aad) {
1763
- const aadBytes = typeof aad === "string" ? textEncoder$1.encode(aad) : aad instanceof Uint8Array ? aad : new Uint8Array(aad);
1764
- if (aadBytes.byteLength > 0) {
1765
- decryptParams.additionalData = aadBytes;
1766
- hasAad = true;
1767
- }
1768
- } else if (payload.aad) {
1769
- throw new Error("[CWebCryptoEncryptionProvider] Payload requires Authenticated Additional Data (AAD) for decryption, but none was provided.");
1786
+ const shouldApplyAad = payload.aad === true || payload.aad === void 0 && (options?.bindRequestContextAad === true || options?.aad !== void 0);
1787
+ if (shouldApplyAad && aad) {
1788
+ const aadBytes = typeof aad === "string" ? textEncoder$1.encode(aad) : aad instanceof Uint8Array ? aad : new Uint8Array(aad);
1789
+ if (aadBytes.byteLength > 0) {
1790
+ decryptParams.additionalData = aadBytes;
1791
+ hasAad = true;
1770
1792
  }
1793
+ } else if (payload.aad && !aad) {
1794
+ throw new Error("[CWebCryptoEncryptionProvider] Payload requires Authenticated Additional Data (AAD) for decryption, but none was provided.");
1771
1795
  }
1772
1796
  let decryptedBuffer;
1773
1797
  try {
@@ -3917,6 +3941,26 @@ async function encryptRows(rows, encryption, apiUrl, idKey) {
3917
3941
  if (!cipherProvider) {
3918
3942
  return rows;
3919
3943
  }
3944
+ if (encryption.fields && encryption.fields.length > 0) {
3945
+ let allEncrypted = true;
3946
+ const fields = encryption.fields;
3947
+ for (let i = 0; i < rows.length; i++) {
3948
+ const item = rows[i];
3949
+ if (item && typeof item === "object") {
3950
+ for (let f = 0; f < fields.length; f++) {
3951
+ const val = item[fields[f]];
3952
+ if (val !== void 0 && val !== null && !cipherProvider.isEncrypted(val)) {
3953
+ allEncrypted = false;
3954
+ break;
3955
+ }
3956
+ }
3957
+ if (!allEncrypted) break;
3958
+ }
3959
+ }
3960
+ if (allEncrypted) {
3961
+ return rows;
3962
+ }
3963
+ }
3920
3964
  const ctx = buildEncryptionContext(encryption, apiUrl);
3921
3965
  if (typeof cipherProvider.warmupKey === "function") {
3922
3966
  await cipherProvider.warmupKey(ctx);
@@ -3933,6 +3977,27 @@ async function encryptRowsChunked(rows, encryption, options, apiUrl, idKey, onCh
3933
3977
  options?.onProgress?.({ processed: rows.length, total: rows.length, percent: 100 });
3934
3978
  return rows;
3935
3979
  }
3980
+ if (!onChunkEncrypted && encryption.fields && encryption.fields.length > 0) {
3981
+ let allEncrypted = true;
3982
+ const fields = encryption.fields;
3983
+ for (let i = 0; i < rows.length; i++) {
3984
+ const item = rows[i];
3985
+ if (item && typeof item === "object") {
3986
+ for (let f = 0; f < fields.length; f++) {
3987
+ const val = item[fields[f]];
3988
+ if (val !== void 0 && val !== null && !cipherProvider.isEncrypted(val)) {
3989
+ allEncrypted = false;
3990
+ break;
3991
+ }
3992
+ }
3993
+ if (!allEncrypted) break;
3994
+ }
3995
+ }
3996
+ if (allEncrypted) {
3997
+ options?.onProgress?.({ processed: rows.length, total: rows.length, percent: 100 });
3998
+ return rows;
3999
+ }
4000
+ }
3936
4001
  const ctx = buildEncryptionContext(encryption, apiUrl);
3937
4002
  if (typeof cipherProvider.warmupKey === "function") {
3938
4003
  await cipherProvider.warmupKey(ctx);
package/dist/index.mjs CHANGED
@@ -799,7 +799,31 @@ class AEncryptionProvider {
799
799
  return this.encryptValue(data, options);
800
800
  }
801
801
  if (Array.isArray(data)) {
802
- if (options?.fields && options.fields.length > 0 || options?.valueEncryption === true) {
802
+ if (data.length === 0) return data;
803
+ if (options?.fields && options.fields.length > 0) {
804
+ const fields = options.fields;
805
+ let hasUnencrypted = false;
806
+ for (let i = 0; i < data.length; i++) {
807
+ const item = data[i];
808
+ if (item && typeof item === "object") {
809
+ for (let f = 0; f < fields.length; f++) {
810
+ const val = item[fields[f]];
811
+ if (val !== void 0 && val !== null && !this.isEncrypted(val)) {
812
+ hasUnencrypted = true;
813
+ break;
814
+ }
815
+ }
816
+ if (hasUnencrypted) break;
817
+ }
818
+ }
819
+ if (!hasUnencrypted) {
820
+ return data;
821
+ }
822
+ await this.warmupKey(options);
823
+ const itemOptions = { ...options, _warmed: true };
824
+ return Promise.all(data.map((item) => this.encrypt(item, itemOptions)));
825
+ }
826
+ if (options?.valueEncryption === true) {
803
827
  await this.warmupKey(options);
804
828
  const itemOptions = { ...options, _warmed: true };
805
829
  return Promise.all(data.map((item) => this.encrypt(item, itemOptions)));
@@ -812,7 +836,7 @@ class AEncryptionProvider {
812
836
  (field) => Object.prototype.hasOwnProperty.call(data, field) && data[field] !== void 0 && data[field] !== null && !this.isEncrypted(data[field])
813
837
  );
814
838
  if (fieldsToEncrypt.length === 0) {
815
- return { ...data };
839
+ return data;
816
840
  }
817
841
  if (!options?._warmed) {
818
842
  await this.warmupKey(options);
@@ -870,12 +894,13 @@ class AEncryptionProvider {
870
894
  return Promise.all(payload.map((item) => this.decrypt(item, options)));
871
895
  }
872
896
  if (typeof payload === "object" && !ArrayBuffer.isView(payload) && !(payload instanceof ArrayBuffer)) {
873
- const result = { ...payload };
874
- const targetKeys = options?.fields && options.fields.length > 0 ? options.fields.filter((f) => Object.prototype.hasOwnProperty.call(result, f)) : Object.keys(result);
875
- const keysToDecrypt = targetKeys.filter((key) => this.isEncrypted(result[key]));
897
+ const targetKeys = options?.fields && options.fields.length > 0 ? options.fields.filter((f) => Object.prototype.hasOwnProperty.call(payload, f)) : Object.keys(payload);
898
+ const keysToDecrypt = targetKeys.filter((key) => this.isEncrypted(payload[key]));
876
899
  if (keysToDecrypt.length === 0) {
877
900
  return payload;
878
901
  }
902
+ const proto = Object.getPrototypeOf(payload);
903
+ const result = proto && proto !== Object.prototype ? Object.assign(Object.create(proto), payload) : { ...payload };
879
904
  const entries = await Promise.all(
880
905
  keysToDecrypt.map(async (key) => {
881
906
  const boundOptions = this._bindFieldAad(payload, key, options);
@@ -1741,16 +1766,15 @@ class CWebCryptoEncryptionProvider extends AEncryptionProvider {
1741
1766
  };
1742
1767
  const aad = this._resolveEffectiveAad(options, true);
1743
1768
  let hasAad = false;
1744
- if (payload.aad || aad) {
1745
- if (aad) {
1746
- const aadBytes = typeof aad === "string" ? textEncoder$1.encode(aad) : aad instanceof Uint8Array ? aad : new Uint8Array(aad);
1747
- if (aadBytes.byteLength > 0) {
1748
- decryptParams.additionalData = aadBytes;
1749
- hasAad = true;
1750
- }
1751
- } else if (payload.aad) {
1752
- throw new Error("[CWebCryptoEncryptionProvider] Payload requires Authenticated Additional Data (AAD) for decryption, but none was provided.");
1769
+ const shouldApplyAad = payload.aad === true || payload.aad === void 0 && (options?.bindRequestContextAad === true || options?.aad !== void 0);
1770
+ if (shouldApplyAad && aad) {
1771
+ const aadBytes = typeof aad === "string" ? textEncoder$1.encode(aad) : aad instanceof Uint8Array ? aad : new Uint8Array(aad);
1772
+ if (aadBytes.byteLength > 0) {
1773
+ decryptParams.additionalData = aadBytes;
1774
+ hasAad = true;
1753
1775
  }
1776
+ } else if (payload.aad && !aad) {
1777
+ throw new Error("[CWebCryptoEncryptionProvider] Payload requires Authenticated Additional Data (AAD) for decryption, but none was provided.");
1754
1778
  }
1755
1779
  let decryptedBuffer;
1756
1780
  try {
@@ -3900,6 +3924,26 @@ async function encryptRows(rows, encryption, apiUrl, idKey) {
3900
3924
  if (!cipherProvider) {
3901
3925
  return rows;
3902
3926
  }
3927
+ if (encryption.fields && encryption.fields.length > 0) {
3928
+ let allEncrypted = true;
3929
+ const fields = encryption.fields;
3930
+ for (let i = 0; i < rows.length; i++) {
3931
+ const item = rows[i];
3932
+ if (item && typeof item === "object") {
3933
+ for (let f = 0; f < fields.length; f++) {
3934
+ const val = item[fields[f]];
3935
+ if (val !== void 0 && val !== null && !cipherProvider.isEncrypted(val)) {
3936
+ allEncrypted = false;
3937
+ break;
3938
+ }
3939
+ }
3940
+ if (!allEncrypted) break;
3941
+ }
3942
+ }
3943
+ if (allEncrypted) {
3944
+ return rows;
3945
+ }
3946
+ }
3903
3947
  const ctx = buildEncryptionContext(encryption, apiUrl);
3904
3948
  if (typeof cipherProvider.warmupKey === "function") {
3905
3949
  await cipherProvider.warmupKey(ctx);
@@ -3916,6 +3960,27 @@ async function encryptRowsChunked(rows, encryption, options, apiUrl, idKey, onCh
3916
3960
  options?.onProgress?.({ processed: rows.length, total: rows.length, percent: 100 });
3917
3961
  return rows;
3918
3962
  }
3963
+ if (!onChunkEncrypted && encryption.fields && encryption.fields.length > 0) {
3964
+ let allEncrypted = true;
3965
+ const fields = encryption.fields;
3966
+ for (let i = 0; i < rows.length; i++) {
3967
+ const item = rows[i];
3968
+ if (item && typeof item === "object") {
3969
+ for (let f = 0; f < fields.length; f++) {
3970
+ const val = item[fields[f]];
3971
+ if (val !== void 0 && val !== null && !cipherProvider.isEncrypted(val)) {
3972
+ allEncrypted = false;
3973
+ break;
3974
+ }
3975
+ }
3976
+ if (!allEncrypted) break;
3977
+ }
3978
+ }
3979
+ if (allEncrypted) {
3980
+ options?.onProgress?.({ processed: rows.length, total: rows.length, percent: 100 });
3981
+ return rows;
3982
+ }
3983
+ }
3919
3984
  const ctx = buildEncryptionContext(encryption, apiUrl);
3920
3985
  if (typeof cipherProvider.warmupKey === "function") {
3921
3986
  await cipherProvider.warmupKey(ctx);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@katlux/providers",
3
- "version": "0.1.1-alpha.120",
3
+ "version": "0.1.1-alpha.121",
4
4
  "description": "Core data calculation and caching providers for the Katlux ecosystem",
5
5
  "author": "Katlux",
6
6
  "license": "Apache-2.0",