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

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);
@@ -878,21 +902,93 @@ class AEncryptionProvider {
878
902
  * - If payload is an array of objects: decrypts each row.
879
903
  * - If payload is an object: decrypts any encrypted value cells across fields.
880
904
  */
905
+ /**
906
+ * Top-level polymorphic decryption method.
907
+ * Automatically performs value-level decryption:
908
+ * - If payload is an encrypted envelope: decrypts directly via decryptValue.
909
+ * - If payload is an array of objects: decrypts each row (batched with local memoization).
910
+ * - If payload is an object: decrypts any encrypted value cells across fields.
911
+ */
881
912
  async decrypt(payload, options) {
882
913
  if (payload === void 0 || payload === null) return payload;
883
914
  if (this.isEncrypted(payload)) {
884
915
  return this.decryptValue(payload, options);
885
916
  }
886
917
  if (Array.isArray(payload)) {
887
- return Promise.all(payload.map((item) => this.decrypt(item, options)));
918
+ if (payload.length === 0) return payload;
919
+ const localMemo = /* @__PURE__ */ new Map();
920
+ const decryptRow = async (item) => {
921
+ if (!item) return item;
922
+ if (this.isEncrypted(item)) {
923
+ const ct = item.ciphertext;
924
+ const iv = item.iv || "";
925
+ const aadPart = options?.aad ? typeof options.aad === "string" ? options.aad : "" : "";
926
+ const memoKey = `${ct}:${iv}:${aadPart}`;
927
+ if (!localMemo.has(memoKey)) {
928
+ localMemo.set(memoKey, this.decryptValue(item, options));
929
+ }
930
+ return localMemo.get(memoKey);
931
+ }
932
+ if (typeof item === "object" && !ArrayBuffer.isView(item) && !(item instanceof ArrayBuffer)) {
933
+ const targetKeys = options?.fields && options.fields.length > 0 ? options.fields.filter((f) => Object.prototype.hasOwnProperty.call(item, f)) : Object.keys(item);
934
+ const keysToDecrypt = targetKeys.filter((key) => this.isEncrypted(item[key]));
935
+ if (keysToDecrypt.length === 0) {
936
+ return item;
937
+ }
938
+ const proto = Object.getPrototypeOf(item);
939
+ const result = proto && proto !== Object.prototype ? Object.assign(Object.create(proto), item) : { ...item };
940
+ await Promise.all(
941
+ keysToDecrypt.map(async (key) => {
942
+ const env = item[key];
943
+ const boundOptions = this._bindFieldAad(item, key, options);
944
+ const hasBoundAad = boundOptions.aad !== options?.aad;
945
+ const ct = env.ciphertext;
946
+ const iv = env.iv || "";
947
+ const aadPart = boundOptions.aad ? typeof boundOptions.aad === "string" ? boundOptions.aad : "" : "";
948
+ const memoKey = `${ct}:${iv}:${aadPart}`;
949
+ if (!localMemo.has(memoKey)) {
950
+ const decryptPromise = this.decryptValue(env, boundOptions).catch((err) => {
951
+ if (hasBoundAad && isAuthOrDecryptionError(err)) {
952
+ return this.decryptValue(env, options);
953
+ }
954
+ throw err;
955
+ });
956
+ localMemo.set(memoKey, decryptPromise);
957
+ }
958
+ const decryptedVal = await localMemo.get(memoKey);
959
+ safeSetProperty(result, key, decryptedVal);
960
+ })
961
+ );
962
+ return result;
963
+ }
964
+ return item;
965
+ };
966
+ if (payload.length <= 100) {
967
+ return Promise.all(payload.map(decryptRow));
968
+ }
969
+ const batchSize = 1e3;
970
+ const results = new Array(payload.length);
971
+ for (let i = 0; i < payload.length; i += batchSize) {
972
+ const end = Math.min(i + batchSize, payload.length);
973
+ const chunk = payload.slice(i, end);
974
+ const chunkRes = await Promise.all(chunk.map(decryptRow));
975
+ for (let j = 0; j < chunkRes.length; j++) {
976
+ results[i + j] = chunkRes[j];
977
+ }
978
+ if (end < payload.length && (i > 0 && i % 5e3 === 0)) {
979
+ await new Promise((r) => setTimeout(r, 0));
980
+ }
981
+ }
982
+ return results;
888
983
  }
889
984
  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]));
985
+ const targetKeys = options?.fields && options.fields.length > 0 ? options.fields.filter((f) => Object.prototype.hasOwnProperty.call(payload, f)) : Object.keys(payload);
986
+ const keysToDecrypt = targetKeys.filter((key) => this.isEncrypted(payload[key]));
893
987
  if (keysToDecrypt.length === 0) {
894
988
  return payload;
895
989
  }
990
+ const proto = Object.getPrototypeOf(payload);
991
+ const result = proto && proto !== Object.prototype ? Object.assign(Object.create(proto), payload) : { ...payload };
896
992
  const entries = await Promise.all(
897
993
  keysToDecrypt.map(async (key) => {
898
994
  const boundOptions = this._bindFieldAad(payload, key, options);
@@ -1758,16 +1854,15 @@ class CWebCryptoEncryptionProvider extends AEncryptionProvider {
1758
1854
  };
1759
1855
  const aad = this._resolveEffectiveAad(options, true);
1760
1856
  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.");
1857
+ const shouldApplyAad = payload.aad === true || payload.aad === void 0 && (options?.bindRequestContextAad === true || options?.aad !== void 0);
1858
+ if (shouldApplyAad && aad) {
1859
+ const aadBytes = typeof aad === "string" ? textEncoder$1.encode(aad) : aad instanceof Uint8Array ? aad : new Uint8Array(aad);
1860
+ if (aadBytes.byteLength > 0) {
1861
+ decryptParams.additionalData = aadBytes;
1862
+ hasAad = true;
1770
1863
  }
1864
+ } else if (payload.aad && !aad) {
1865
+ throw new Error("[CWebCryptoEncryptionProvider] Payload requires Authenticated Additional Data (AAD) for decryption, but none was provided.");
1771
1866
  }
1772
1867
  let decryptedBuffer;
1773
1868
  try {
@@ -3917,6 +4012,26 @@ async function encryptRows(rows, encryption, apiUrl, idKey) {
3917
4012
  if (!cipherProvider) {
3918
4013
  return rows;
3919
4014
  }
4015
+ if (encryption.fields && encryption.fields.length > 0) {
4016
+ let allEncrypted = true;
4017
+ const fields = encryption.fields;
4018
+ for (let i = 0; i < rows.length; i++) {
4019
+ const item = rows[i];
4020
+ if (item && typeof item === "object") {
4021
+ for (let f = 0; f < fields.length; f++) {
4022
+ const val = item[fields[f]];
4023
+ if (val !== void 0 && val !== null && !cipherProvider.isEncrypted(val)) {
4024
+ allEncrypted = false;
4025
+ break;
4026
+ }
4027
+ }
4028
+ if (!allEncrypted) break;
4029
+ }
4030
+ }
4031
+ if (allEncrypted) {
4032
+ return rows;
4033
+ }
4034
+ }
3920
4035
  const ctx = buildEncryptionContext(encryption, apiUrl);
3921
4036
  if (typeof cipherProvider.warmupKey === "function") {
3922
4037
  await cipherProvider.warmupKey(ctx);
@@ -3933,6 +4048,27 @@ async function encryptRowsChunked(rows, encryption, options, apiUrl, idKey, onCh
3933
4048
  options?.onProgress?.({ processed: rows.length, total: rows.length, percent: 100 });
3934
4049
  return rows;
3935
4050
  }
4051
+ if (!onChunkEncrypted && encryption.fields && encryption.fields.length > 0) {
4052
+ let allEncrypted = true;
4053
+ const fields = encryption.fields;
4054
+ for (let i = 0; i < rows.length; i++) {
4055
+ const item = rows[i];
4056
+ if (item && typeof item === "object") {
4057
+ for (let f = 0; f < fields.length; f++) {
4058
+ const val = item[fields[f]];
4059
+ if (val !== void 0 && val !== null && !cipherProvider.isEncrypted(val)) {
4060
+ allEncrypted = false;
4061
+ break;
4062
+ }
4063
+ }
4064
+ if (!allEncrypted) break;
4065
+ }
4066
+ }
4067
+ if (allEncrypted) {
4068
+ options?.onProgress?.({ processed: rows.length, total: rows.length, percent: 100 });
4069
+ return rows;
4070
+ }
4071
+ }
3936
4072
  const ctx = buildEncryptionContext(encryption, apiUrl);
3937
4073
  if (typeof cipherProvider.warmupKey === "function") {
3938
4074
  await cipherProvider.warmupKey(ctx);
@@ -4732,16 +4868,19 @@ class CFlatClientDataProvider extends AEncryptedClientProvider {
4732
4868
  this.error = null;
4733
4869
  try {
4734
4870
  this._computeFilteredAndSorted();
4735
- if (this.pageData && this.pageData.length > 0) {
4736
- const { rows, decrypted } = await this.decryptRows(this.pageData);
4871
+ if (this._rowMap && this._rowMap.length > 0) {
4872
+ const { rows, decrypted } = await this.decryptRows(this._rowMap);
4737
4873
  if (decrypted) {
4738
- this.pageData = rows;
4739
- const startIndex = this.pageSize <= 0 ? 0 : (this.currentPage - 1) * this.pageSize;
4740
- for (let i = 0; i < rows.length; i++) {
4741
- this._rowMap[startIndex + i] = rows[i];
4742
- }
4874
+ this._rowMap = rows;
4743
4875
  }
4744
4876
  }
4877
+ if (this.pageSize <= 0) {
4878
+ this.pageData = this._rowMap;
4879
+ } else {
4880
+ const startIndex = (this.currentPage - 1) * this.pageSize;
4881
+ const endIndex = startIndex + this.pageSize;
4882
+ this.pageData = this._rowMap.slice(startIndex, endIndex);
4883
+ }
4745
4884
  } catch (err) {
4746
4885
  this.error = err;
4747
4886
  console.warn("[Katlux Providers] CFlatClientDataProvider loadPageData error", err);
@@ -7356,6 +7495,7 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7356
7495
  _sortedNodeIdsCache = null;
7357
7496
  _filteredNodesCache = null;
7358
7497
  _allVisibleRowsCache = null;
7498
+ _decryptedNodeMap = /* @__PURE__ */ new Map();
7359
7499
  invalidateStaticCache() {
7360
7500
  this._staticNodesCache = null;
7361
7501
  this._nodeMapCache = null;
@@ -7403,6 +7543,7 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7403
7543
  this.updatePageData();
7404
7544
  }
7405
7545
  _applyTreeData(rows) {
7546
+ this._decryptedNodeMap.clear();
7406
7547
  this.invalidateStaticCache();
7407
7548
  this.allData = rows;
7408
7549
  this._filterDirty = true;
@@ -7706,22 +7847,30 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7706
7847
  const filteredNodes = this._filteredNodesCache;
7707
7848
  this.filteredData = filteredNodes;
7708
7849
  const paginated = this.paginateNodes(filteredNodes);
7709
- this.pageData = paginated.map((node) => ({
7710
- ...node.data,
7711
- ...node,
7712
- __treeNode: node
7713
- }));
7850
+ this.pageData = paginated.map((node) => {
7851
+ const decrypted = this.isEncryptionEnabled ? this._decryptedNodeMap.get(node.id) : void 0;
7852
+ const nodeData = decrypted ? { ...node.data, ...decrypted } : node.data;
7853
+ return {
7854
+ ...nodeData,
7855
+ ...node,
7856
+ __treeNode: node
7857
+ };
7858
+ });
7714
7859
  if (this.allData.length > 0) {
7715
7860
  this.initialLoad = false;
7716
7861
  }
7717
7862
  return;
7718
7863
  }
7719
7864
  const visible = this.visibleRows;
7720
- this.pageData = visible.map((node) => ({
7721
- ...node.data,
7722
- ...node,
7723
- __treeNode: node
7724
- }));
7865
+ this.pageData = visible.map((node) => {
7866
+ const decrypted = this.isEncryptionEnabled ? this._decryptedNodeMap.get(node.id) : void 0;
7867
+ const nodeData = decrypted ? { ...node.data, ...decrypted } : node.data;
7868
+ return {
7869
+ ...nodeData,
7870
+ ...node,
7871
+ __treeNode: node
7872
+ };
7873
+ });
7725
7874
  if (this.allData.length > 0) {
7726
7875
  this.initialLoad = false;
7727
7876
  }
@@ -7757,18 +7906,37 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7757
7906
  this.loading = true;
7758
7907
  this.error = null;
7759
7908
  try {
7760
- this.updatePageData();
7761
- if (this.pageData && this.pageData.length > 0) {
7762
- const { rows, decrypted } = await this.decryptRows(this.pageData);
7909
+ const visibleNodes = this.allVisibleNodes.map((node) => {
7910
+ const decrypted = this._decryptedNodeMap.get(node.id);
7911
+ const nodeData = decrypted ? { ...node.data, ...decrypted } : node.data;
7912
+ return {
7913
+ ...nodeData,
7914
+ ...node,
7915
+ __treeNode: node
7916
+ };
7917
+ });
7918
+ if (visibleNodes.length > 0) {
7919
+ const { rows, decrypted } = await this.decryptRows(visibleNodes);
7763
7920
  if (decrypted) {
7764
- this.pageData = rows;
7765
- const startIndex = this.pageSize <= 0 ? 0 : (this.currentPage - 1) * this.pageSize;
7766
- if (this._allVisibleRowsCache) {
7767
- for (let i = 0; i < rows.length; i++) {
7768
- this._allVisibleRowsCache[startIndex + i] = rows[i];
7921
+ for (const r of rows) {
7922
+ const id = r[this.idKey] ?? r.id;
7923
+ if (id !== void 0 && id !== null) {
7924
+ this._decryptedNodeMap.set(id, r);
7769
7925
  }
7770
7926
  }
7771
7927
  }
7928
+ this._allVisibleRowsCache = decrypted ? rows : visibleNodes;
7929
+ } else {
7930
+ this._allVisibleRowsCache = [];
7931
+ }
7932
+ if (this.pageSize <= 0) {
7933
+ this.pageData = [...this._allVisibleRowsCache];
7934
+ } else {
7935
+ const startIndex = (this.currentPage - 1) * this.pageSize;
7936
+ this.pageData = this._allVisibleRowsCache.slice(startIndex, startIndex + this.pageSize);
7937
+ }
7938
+ if (this.allData.length > 0) {
7939
+ this.initialLoad = false;
7772
7940
  }
7773
7941
  } catch (err) {
7774
7942
  this.error = err;
@@ -7781,6 +7949,7 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7781
7949
  }
7782
7950
  clearEncryption() {
7783
7951
  super.clearEncryption();
7952
+ this._decryptedNodeMap.clear();
7784
7953
  this._filterDirty = true;
7785
7954
  this._sortDirty = true;
7786
7955
  this._filteredNodesCache = null;
@@ -7821,11 +7990,15 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7821
7990
  }
7822
7991
  get allVisibleRows() {
7823
7992
  if (!this._allVisibleRowsCache) {
7824
- this._allVisibleRowsCache = this.allVisibleNodes.map((node) => ({
7825
- ...node.data,
7826
- ...node,
7827
- __treeNode: node
7828
- }));
7993
+ this._allVisibleRowsCache = this.allVisibleNodes.map((node) => {
7994
+ const decrypted = this.isEncryptionEnabled ? this._decryptedNodeMap.get(node.id) : void 0;
7995
+ const nodeData = decrypted ? { ...node.data, ...decrypted } : node.data;
7996
+ return {
7997
+ ...nodeData,
7998
+ ...node,
7999
+ __treeNode: node
8000
+ };
8001
+ });
7829
8002
  if (this.isEncryptionEnabled && this.pageData && this.pageData.length > 0) {
7830
8003
  const startIndex = this.pageSize <= 0 ? 0 : (this.currentPage - 1) * this.pageSize;
7831
8004
  for (let i = 0; i < this.pageData.length; i++) {
@@ -7856,6 +8029,9 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7856
8029
  }
7857
8030
  this.updatePageData();
7858
8031
  this.notify();
8032
+ if (this.isEncryptionEnabled) {
8033
+ this.loadPageData();
8034
+ }
7859
8035
  }
7860
8036
  expand(nodeId) {
7861
8037
  if (this.filter && this.filter.active) {
@@ -7865,6 +8041,9 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7865
8041
  this.expandedNodes.add(nodeId);
7866
8042
  this.updatePageData();
7867
8043
  this.notify();
8044
+ if (this.isEncryptionEnabled) {
8045
+ this.loadPageData();
8046
+ }
7868
8047
  }
7869
8048
  collapse(nodeId) {
7870
8049
  if (this.filter && this.filter.active) {
@@ -7874,6 +8053,9 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7874
8053
  this.expandedNodes.delete(nodeId);
7875
8054
  this.updatePageData();
7876
8055
  this.notify();
8056
+ if (this.isEncryptionEnabled) {
8057
+ this.loadPageData();
8058
+ }
7877
8059
  }
7878
8060
  expandNode(nodeId) {
7879
8061
  this.expand(nodeId);
@@ -7890,6 +8072,9 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7890
8072
  this.expandedNodes = new Set(allIds);
7891
8073
  this.updatePageData();
7892
8074
  this.notify();
8075
+ if (this.isEncryptionEnabled) {
8076
+ this.loadPageData();
8077
+ }
7893
8078
  }
7894
8079
  collapseAll() {
7895
8080
  if (this.filter && this.filter.active) {
@@ -7899,6 +8084,9 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7899
8084
  this.expandedNodes = /* @__PURE__ */ new Set();
7900
8085
  this.updatePageData();
7901
8086
  this.notify();
8087
+ if (this.isEncryptionEnabled) {
8088
+ this.loadPageData();
8089
+ }
7902
8090
  }
7903
8091
  getNode(nodeId) {
7904
8092
  const staticNode = this._nodeMap.get(nodeId);
package/dist/index.d.cts CHANGED
@@ -803,6 +803,7 @@ declare class CTreeClientDataProvider<TRow extends TDataRow = TDataRow> extends
803
803
  private _sortedNodeIdsCache;
804
804
  private _filteredNodesCache;
805
805
  private _allVisibleRowsCache;
806
+ private _decryptedNodeMap;
806
807
  invalidateStaticCache(): void;
807
808
  setFilter(filter: IDataFilter | null, immediate?: boolean): void;
808
809
  protected onFilterUpdated(): void;
@@ -1622,6 +1623,13 @@ declare abstract class AEncryptionProvider implements IEncryptionProvider {
1622
1623
  * - If payload is an array of objects: decrypts each row.
1623
1624
  * - If payload is an object: decrypts any encrypted value cells across fields.
1624
1625
  */
1626
+ /**
1627
+ * Top-level polymorphic decryption method.
1628
+ * Automatically performs value-level decryption:
1629
+ * - If payload is an encrypted envelope: decrypts directly via decryptValue.
1630
+ * - If payload is an array of objects: decrypts each row (batched with local memoization).
1631
+ * - If payload is an object: decrypts any encrypted value cells across fields.
1632
+ */
1625
1633
  decrypt(payload: any, options?: IEncryptionContext): Promise<any>;
1626
1634
  }
1627
1635
 
package/dist/index.d.mts CHANGED
@@ -803,6 +803,7 @@ declare class CTreeClientDataProvider<TRow extends TDataRow = TDataRow> extends
803
803
  private _sortedNodeIdsCache;
804
804
  private _filteredNodesCache;
805
805
  private _allVisibleRowsCache;
806
+ private _decryptedNodeMap;
806
807
  invalidateStaticCache(): void;
807
808
  setFilter(filter: IDataFilter | null, immediate?: boolean): void;
808
809
  protected onFilterUpdated(): void;
@@ -1622,6 +1623,13 @@ declare abstract class AEncryptionProvider implements IEncryptionProvider {
1622
1623
  * - If payload is an array of objects: decrypts each row.
1623
1624
  * - If payload is an object: decrypts any encrypted value cells across fields.
1624
1625
  */
1626
+ /**
1627
+ * Top-level polymorphic decryption method.
1628
+ * Automatically performs value-level decryption:
1629
+ * - If payload is an encrypted envelope: decrypts directly via decryptValue.
1630
+ * - If payload is an array of objects: decrypts each row (batched with local memoization).
1631
+ * - If payload is an object: decrypts any encrypted value cells across fields.
1632
+ */
1625
1633
  decrypt(payload: any, options?: IEncryptionContext): Promise<any>;
1626
1634
  }
1627
1635
 
package/dist/index.d.ts CHANGED
@@ -803,6 +803,7 @@ declare class CTreeClientDataProvider<TRow extends TDataRow = TDataRow> extends
803
803
  private _sortedNodeIdsCache;
804
804
  private _filteredNodesCache;
805
805
  private _allVisibleRowsCache;
806
+ private _decryptedNodeMap;
806
807
  invalidateStaticCache(): void;
807
808
  setFilter(filter: IDataFilter | null, immediate?: boolean): void;
808
809
  protected onFilterUpdated(): void;
@@ -1622,6 +1623,13 @@ declare abstract class AEncryptionProvider implements IEncryptionProvider {
1622
1623
  * - If payload is an array of objects: decrypts each row.
1623
1624
  * - If payload is an object: decrypts any encrypted value cells across fields.
1624
1625
  */
1626
+ /**
1627
+ * Top-level polymorphic decryption method.
1628
+ * Automatically performs value-level decryption:
1629
+ * - If payload is an encrypted envelope: decrypts directly via decryptValue.
1630
+ * - If payload is an array of objects: decrypts each row (batched with local memoization).
1631
+ * - If payload is an object: decrypts any encrypted value cells across fields.
1632
+ */
1625
1633
  decrypt(payload: any, options?: IEncryptionContext): Promise<any>;
1626
1634
  }
1627
1635
 
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);
@@ -861,21 +885,93 @@ class AEncryptionProvider {
861
885
  * - If payload is an array of objects: decrypts each row.
862
886
  * - If payload is an object: decrypts any encrypted value cells across fields.
863
887
  */
888
+ /**
889
+ * Top-level polymorphic decryption method.
890
+ * Automatically performs value-level decryption:
891
+ * - If payload is an encrypted envelope: decrypts directly via decryptValue.
892
+ * - If payload is an array of objects: decrypts each row (batched with local memoization).
893
+ * - If payload is an object: decrypts any encrypted value cells across fields.
894
+ */
864
895
  async decrypt(payload, options) {
865
896
  if (payload === void 0 || payload === null) return payload;
866
897
  if (this.isEncrypted(payload)) {
867
898
  return this.decryptValue(payload, options);
868
899
  }
869
900
  if (Array.isArray(payload)) {
870
- return Promise.all(payload.map((item) => this.decrypt(item, options)));
901
+ if (payload.length === 0) return payload;
902
+ const localMemo = /* @__PURE__ */ new Map();
903
+ const decryptRow = async (item) => {
904
+ if (!item) return item;
905
+ if (this.isEncrypted(item)) {
906
+ const ct = item.ciphertext;
907
+ const iv = item.iv || "";
908
+ const aadPart = options?.aad ? typeof options.aad === "string" ? options.aad : "" : "";
909
+ const memoKey = `${ct}:${iv}:${aadPart}`;
910
+ if (!localMemo.has(memoKey)) {
911
+ localMemo.set(memoKey, this.decryptValue(item, options));
912
+ }
913
+ return localMemo.get(memoKey);
914
+ }
915
+ if (typeof item === "object" && !ArrayBuffer.isView(item) && !(item instanceof ArrayBuffer)) {
916
+ const targetKeys = options?.fields && options.fields.length > 0 ? options.fields.filter((f) => Object.prototype.hasOwnProperty.call(item, f)) : Object.keys(item);
917
+ const keysToDecrypt = targetKeys.filter((key) => this.isEncrypted(item[key]));
918
+ if (keysToDecrypt.length === 0) {
919
+ return item;
920
+ }
921
+ const proto = Object.getPrototypeOf(item);
922
+ const result = proto && proto !== Object.prototype ? Object.assign(Object.create(proto), item) : { ...item };
923
+ await Promise.all(
924
+ keysToDecrypt.map(async (key) => {
925
+ const env = item[key];
926
+ const boundOptions = this._bindFieldAad(item, key, options);
927
+ const hasBoundAad = boundOptions.aad !== options?.aad;
928
+ const ct = env.ciphertext;
929
+ const iv = env.iv || "";
930
+ const aadPart = boundOptions.aad ? typeof boundOptions.aad === "string" ? boundOptions.aad : "" : "";
931
+ const memoKey = `${ct}:${iv}:${aadPart}`;
932
+ if (!localMemo.has(memoKey)) {
933
+ const decryptPromise = this.decryptValue(env, boundOptions).catch((err) => {
934
+ if (hasBoundAad && isAuthOrDecryptionError(err)) {
935
+ return this.decryptValue(env, options);
936
+ }
937
+ throw err;
938
+ });
939
+ localMemo.set(memoKey, decryptPromise);
940
+ }
941
+ const decryptedVal = await localMemo.get(memoKey);
942
+ safeSetProperty(result, key, decryptedVal);
943
+ })
944
+ );
945
+ return result;
946
+ }
947
+ return item;
948
+ };
949
+ if (payload.length <= 100) {
950
+ return Promise.all(payload.map(decryptRow));
951
+ }
952
+ const batchSize = 1e3;
953
+ const results = new Array(payload.length);
954
+ for (let i = 0; i < payload.length; i += batchSize) {
955
+ const end = Math.min(i + batchSize, payload.length);
956
+ const chunk = payload.slice(i, end);
957
+ const chunkRes = await Promise.all(chunk.map(decryptRow));
958
+ for (let j = 0; j < chunkRes.length; j++) {
959
+ results[i + j] = chunkRes[j];
960
+ }
961
+ if (end < payload.length && (i > 0 && i % 5e3 === 0)) {
962
+ await new Promise((r) => setTimeout(r, 0));
963
+ }
964
+ }
965
+ return results;
871
966
  }
872
967
  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]));
968
+ const targetKeys = options?.fields && options.fields.length > 0 ? options.fields.filter((f) => Object.prototype.hasOwnProperty.call(payload, f)) : Object.keys(payload);
969
+ const keysToDecrypt = targetKeys.filter((key) => this.isEncrypted(payload[key]));
876
970
  if (keysToDecrypt.length === 0) {
877
971
  return payload;
878
972
  }
973
+ const proto = Object.getPrototypeOf(payload);
974
+ const result = proto && proto !== Object.prototype ? Object.assign(Object.create(proto), payload) : { ...payload };
879
975
  const entries = await Promise.all(
880
976
  keysToDecrypt.map(async (key) => {
881
977
  const boundOptions = this._bindFieldAad(payload, key, options);
@@ -1741,16 +1837,15 @@ class CWebCryptoEncryptionProvider extends AEncryptionProvider {
1741
1837
  };
1742
1838
  const aad = this._resolveEffectiveAad(options, true);
1743
1839
  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.");
1840
+ const shouldApplyAad = payload.aad === true || payload.aad === void 0 && (options?.bindRequestContextAad === true || options?.aad !== void 0);
1841
+ if (shouldApplyAad && aad) {
1842
+ const aadBytes = typeof aad === "string" ? textEncoder$1.encode(aad) : aad instanceof Uint8Array ? aad : new Uint8Array(aad);
1843
+ if (aadBytes.byteLength > 0) {
1844
+ decryptParams.additionalData = aadBytes;
1845
+ hasAad = true;
1753
1846
  }
1847
+ } else if (payload.aad && !aad) {
1848
+ throw new Error("[CWebCryptoEncryptionProvider] Payload requires Authenticated Additional Data (AAD) for decryption, but none was provided.");
1754
1849
  }
1755
1850
  let decryptedBuffer;
1756
1851
  try {
@@ -3900,6 +3995,26 @@ async function encryptRows(rows, encryption, apiUrl, idKey) {
3900
3995
  if (!cipherProvider) {
3901
3996
  return rows;
3902
3997
  }
3998
+ if (encryption.fields && encryption.fields.length > 0) {
3999
+ let allEncrypted = true;
4000
+ const fields = encryption.fields;
4001
+ for (let i = 0; i < rows.length; i++) {
4002
+ const item = rows[i];
4003
+ if (item && typeof item === "object") {
4004
+ for (let f = 0; f < fields.length; f++) {
4005
+ const val = item[fields[f]];
4006
+ if (val !== void 0 && val !== null && !cipherProvider.isEncrypted(val)) {
4007
+ allEncrypted = false;
4008
+ break;
4009
+ }
4010
+ }
4011
+ if (!allEncrypted) break;
4012
+ }
4013
+ }
4014
+ if (allEncrypted) {
4015
+ return rows;
4016
+ }
4017
+ }
3903
4018
  const ctx = buildEncryptionContext(encryption, apiUrl);
3904
4019
  if (typeof cipherProvider.warmupKey === "function") {
3905
4020
  await cipherProvider.warmupKey(ctx);
@@ -3916,6 +4031,27 @@ async function encryptRowsChunked(rows, encryption, options, apiUrl, idKey, onCh
3916
4031
  options?.onProgress?.({ processed: rows.length, total: rows.length, percent: 100 });
3917
4032
  return rows;
3918
4033
  }
4034
+ if (!onChunkEncrypted && encryption.fields && encryption.fields.length > 0) {
4035
+ let allEncrypted = true;
4036
+ const fields = encryption.fields;
4037
+ for (let i = 0; i < rows.length; i++) {
4038
+ const item = rows[i];
4039
+ if (item && typeof item === "object") {
4040
+ for (let f = 0; f < fields.length; f++) {
4041
+ const val = item[fields[f]];
4042
+ if (val !== void 0 && val !== null && !cipherProvider.isEncrypted(val)) {
4043
+ allEncrypted = false;
4044
+ break;
4045
+ }
4046
+ }
4047
+ if (!allEncrypted) break;
4048
+ }
4049
+ }
4050
+ if (allEncrypted) {
4051
+ options?.onProgress?.({ processed: rows.length, total: rows.length, percent: 100 });
4052
+ return rows;
4053
+ }
4054
+ }
3919
4055
  const ctx = buildEncryptionContext(encryption, apiUrl);
3920
4056
  if (typeof cipherProvider.warmupKey === "function") {
3921
4057
  await cipherProvider.warmupKey(ctx);
@@ -4715,16 +4851,19 @@ class CFlatClientDataProvider extends AEncryptedClientProvider {
4715
4851
  this.error = null;
4716
4852
  try {
4717
4853
  this._computeFilteredAndSorted();
4718
- if (this.pageData && this.pageData.length > 0) {
4719
- const { rows, decrypted } = await this.decryptRows(this.pageData);
4854
+ if (this._rowMap && this._rowMap.length > 0) {
4855
+ const { rows, decrypted } = await this.decryptRows(this._rowMap);
4720
4856
  if (decrypted) {
4721
- this.pageData = rows;
4722
- const startIndex = this.pageSize <= 0 ? 0 : (this.currentPage - 1) * this.pageSize;
4723
- for (let i = 0; i < rows.length; i++) {
4724
- this._rowMap[startIndex + i] = rows[i];
4725
- }
4857
+ this._rowMap = rows;
4726
4858
  }
4727
4859
  }
4860
+ if (this.pageSize <= 0) {
4861
+ this.pageData = this._rowMap;
4862
+ } else {
4863
+ const startIndex = (this.currentPage - 1) * this.pageSize;
4864
+ const endIndex = startIndex + this.pageSize;
4865
+ this.pageData = this._rowMap.slice(startIndex, endIndex);
4866
+ }
4728
4867
  } catch (err) {
4729
4868
  this.error = err;
4730
4869
  console.warn("[Katlux Providers] CFlatClientDataProvider loadPageData error", err);
@@ -7339,6 +7478,7 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7339
7478
  _sortedNodeIdsCache = null;
7340
7479
  _filteredNodesCache = null;
7341
7480
  _allVisibleRowsCache = null;
7481
+ _decryptedNodeMap = /* @__PURE__ */ new Map();
7342
7482
  invalidateStaticCache() {
7343
7483
  this._staticNodesCache = null;
7344
7484
  this._nodeMapCache = null;
@@ -7386,6 +7526,7 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7386
7526
  this.updatePageData();
7387
7527
  }
7388
7528
  _applyTreeData(rows) {
7529
+ this._decryptedNodeMap.clear();
7389
7530
  this.invalidateStaticCache();
7390
7531
  this.allData = rows;
7391
7532
  this._filterDirty = true;
@@ -7689,22 +7830,30 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7689
7830
  const filteredNodes = this._filteredNodesCache;
7690
7831
  this.filteredData = filteredNodes;
7691
7832
  const paginated = this.paginateNodes(filteredNodes);
7692
- this.pageData = paginated.map((node) => ({
7693
- ...node.data,
7694
- ...node,
7695
- __treeNode: node
7696
- }));
7833
+ this.pageData = paginated.map((node) => {
7834
+ const decrypted = this.isEncryptionEnabled ? this._decryptedNodeMap.get(node.id) : void 0;
7835
+ const nodeData = decrypted ? { ...node.data, ...decrypted } : node.data;
7836
+ return {
7837
+ ...nodeData,
7838
+ ...node,
7839
+ __treeNode: node
7840
+ };
7841
+ });
7697
7842
  if (this.allData.length > 0) {
7698
7843
  this.initialLoad = false;
7699
7844
  }
7700
7845
  return;
7701
7846
  }
7702
7847
  const visible = this.visibleRows;
7703
- this.pageData = visible.map((node) => ({
7704
- ...node.data,
7705
- ...node,
7706
- __treeNode: node
7707
- }));
7848
+ this.pageData = visible.map((node) => {
7849
+ const decrypted = this.isEncryptionEnabled ? this._decryptedNodeMap.get(node.id) : void 0;
7850
+ const nodeData = decrypted ? { ...node.data, ...decrypted } : node.data;
7851
+ return {
7852
+ ...nodeData,
7853
+ ...node,
7854
+ __treeNode: node
7855
+ };
7856
+ });
7708
7857
  if (this.allData.length > 0) {
7709
7858
  this.initialLoad = false;
7710
7859
  }
@@ -7740,18 +7889,37 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7740
7889
  this.loading = true;
7741
7890
  this.error = null;
7742
7891
  try {
7743
- this.updatePageData();
7744
- if (this.pageData && this.pageData.length > 0) {
7745
- const { rows, decrypted } = await this.decryptRows(this.pageData);
7892
+ const visibleNodes = this.allVisibleNodes.map((node) => {
7893
+ const decrypted = this._decryptedNodeMap.get(node.id);
7894
+ const nodeData = decrypted ? { ...node.data, ...decrypted } : node.data;
7895
+ return {
7896
+ ...nodeData,
7897
+ ...node,
7898
+ __treeNode: node
7899
+ };
7900
+ });
7901
+ if (visibleNodes.length > 0) {
7902
+ const { rows, decrypted } = await this.decryptRows(visibleNodes);
7746
7903
  if (decrypted) {
7747
- this.pageData = rows;
7748
- const startIndex = this.pageSize <= 0 ? 0 : (this.currentPage - 1) * this.pageSize;
7749
- if (this._allVisibleRowsCache) {
7750
- for (let i = 0; i < rows.length; i++) {
7751
- this._allVisibleRowsCache[startIndex + i] = rows[i];
7904
+ for (const r of rows) {
7905
+ const id = r[this.idKey] ?? r.id;
7906
+ if (id !== void 0 && id !== null) {
7907
+ this._decryptedNodeMap.set(id, r);
7752
7908
  }
7753
7909
  }
7754
7910
  }
7911
+ this._allVisibleRowsCache = decrypted ? rows : visibleNodes;
7912
+ } else {
7913
+ this._allVisibleRowsCache = [];
7914
+ }
7915
+ if (this.pageSize <= 0) {
7916
+ this.pageData = [...this._allVisibleRowsCache];
7917
+ } else {
7918
+ const startIndex = (this.currentPage - 1) * this.pageSize;
7919
+ this.pageData = this._allVisibleRowsCache.slice(startIndex, startIndex + this.pageSize);
7920
+ }
7921
+ if (this.allData.length > 0) {
7922
+ this.initialLoad = false;
7755
7923
  }
7756
7924
  } catch (err) {
7757
7925
  this.error = err;
@@ -7764,6 +7932,7 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7764
7932
  }
7765
7933
  clearEncryption() {
7766
7934
  super.clearEncryption();
7935
+ this._decryptedNodeMap.clear();
7767
7936
  this._filterDirty = true;
7768
7937
  this._sortDirty = true;
7769
7938
  this._filteredNodesCache = null;
@@ -7804,11 +7973,15 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7804
7973
  }
7805
7974
  get allVisibleRows() {
7806
7975
  if (!this._allVisibleRowsCache) {
7807
- this._allVisibleRowsCache = this.allVisibleNodes.map((node) => ({
7808
- ...node.data,
7809
- ...node,
7810
- __treeNode: node
7811
- }));
7976
+ this._allVisibleRowsCache = this.allVisibleNodes.map((node) => {
7977
+ const decrypted = this.isEncryptionEnabled ? this._decryptedNodeMap.get(node.id) : void 0;
7978
+ const nodeData = decrypted ? { ...node.data, ...decrypted } : node.data;
7979
+ return {
7980
+ ...nodeData,
7981
+ ...node,
7982
+ __treeNode: node
7983
+ };
7984
+ });
7812
7985
  if (this.isEncryptionEnabled && this.pageData && this.pageData.length > 0) {
7813
7986
  const startIndex = this.pageSize <= 0 ? 0 : (this.currentPage - 1) * this.pageSize;
7814
7987
  for (let i = 0; i < this.pageData.length; i++) {
@@ -7839,6 +8012,9 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7839
8012
  }
7840
8013
  this.updatePageData();
7841
8014
  this.notify();
8015
+ if (this.isEncryptionEnabled) {
8016
+ this.loadPageData();
8017
+ }
7842
8018
  }
7843
8019
  expand(nodeId) {
7844
8020
  if (this.filter && this.filter.active) {
@@ -7848,6 +8024,9 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7848
8024
  this.expandedNodes.add(nodeId);
7849
8025
  this.updatePageData();
7850
8026
  this.notify();
8027
+ if (this.isEncryptionEnabled) {
8028
+ this.loadPageData();
8029
+ }
7851
8030
  }
7852
8031
  collapse(nodeId) {
7853
8032
  if (this.filter && this.filter.active) {
@@ -7857,6 +8036,9 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7857
8036
  this.expandedNodes.delete(nodeId);
7858
8037
  this.updatePageData();
7859
8038
  this.notify();
8039
+ if (this.isEncryptionEnabled) {
8040
+ this.loadPageData();
8041
+ }
7860
8042
  }
7861
8043
  expandNode(nodeId) {
7862
8044
  this.expand(nodeId);
@@ -7873,6 +8055,9 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7873
8055
  this.expandedNodes = new Set(allIds);
7874
8056
  this.updatePageData();
7875
8057
  this.notify();
8058
+ if (this.isEncryptionEnabled) {
8059
+ this.loadPageData();
8060
+ }
7876
8061
  }
7877
8062
  collapseAll() {
7878
8063
  if (this.filter && this.filter.active) {
@@ -7882,6 +8067,9 @@ class CTreeClientDataProvider extends CFlatClientDataProvider {
7882
8067
  this.expandedNodes = /* @__PURE__ */ new Set();
7883
8068
  this.updatePageData();
7884
8069
  this.notify();
8070
+ if (this.isEncryptionEnabled) {
8071
+ this.loadPageData();
8072
+ }
7885
8073
  }
7886
8074
  getNode(nodeId) {
7887
8075
  const staticNode = this._nodeMap.get(nodeId);
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.122",
4
4
  "description": "Core data calculation and caching providers for the Katlux ecosystem",
5
5
  "author": "Katlux",
6
6
  "license": "Apache-2.0",