@peerbit/document 15.0.14 → 15.0.15

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/src/search.ts CHANGED
@@ -44,6 +44,7 @@ import { AbortError, TimeoutError, waitFor } from "@peerbit/time";
44
44
  import pDefer, { type DeferredPromise } from "p-defer";
45
45
  import { concat, equals, fromString } from "uint8arrays";
46
46
  import { copySerialization } from "./borsh.js";
47
+ import { detachPublicKeysForCallback } from "./callback-detachment.js";
47
48
  import { MAX_BATCH_SIZE } from "./constants.js";
48
49
  import type { DocumentEvents, DocumentsChange } from "./events.js";
49
50
  import type { QueryPredictor } from "./most-common-query-predictor.js";
@@ -74,6 +75,7 @@ import {
74
75
  canPrepareDocumentTransformWithAppendFacts,
75
76
  documentTransformPreservesFieldPath,
76
77
  getDocumentTransformDescriptor,
78
+ isBuiltInDocumentTransformer,
77
79
  } from "./transform.js";
78
80
 
79
81
  const WARNING_WHEN_ITERATING_FOR_MORE_THAN = 1e5;
@@ -1050,6 +1052,7 @@ export class DocumentIndex<
1050
1052
  // transform options
1051
1053
  transformer: Transformer<T, I>;
1052
1054
  private transformerIsIdentity = false;
1055
+ private detachTransformerFacts = false;
1053
1056
  private nativeTransformDescriptor?: DocumentTransformDescriptor;
1054
1057
  private nativeTransformProjectionPlan?: SimpleDocumentProjectionPlan;
1055
1058
  private nativeBackboneDocumentProjection?: NativeBackboneDocumentProjection;
@@ -1631,6 +1634,9 @@ export class DocumentIndex<
1631
1634
  this.nativeTransformDescriptor = hasTransformFunction
1632
1635
  ? getDocumentTransformDescriptor(transformOptions.transform)
1633
1636
  : undefined;
1637
+ this.detachTransformerFacts =
1638
+ hasTransformFunction &&
1639
+ !isBuiltInDocumentTransformer(transformOptions.transform);
1634
1640
  this.nativeTransformProjectionPlan = createSimpleProjectionPlan(
1635
1641
  getSchema(this.documentType),
1636
1642
  indexedSchema,
@@ -1827,6 +1833,36 @@ export class DocumentIndex<
1827
1833
  );
1828
1834
  }
1829
1835
 
1836
+ private transformWithDetachedFacts(
1837
+ value: T,
1838
+ context: types.Context,
1839
+ facts?: DocumentTransformFacts,
1840
+ ): MaybePromise<I> {
1841
+ let callbackFacts = facts;
1842
+ if (facts?.entryPublicKeys && this.detachTransformerFacts) {
1843
+ const sourceKeys = facts.entryPublicKeys;
1844
+ let callbackKeys: readonly PublicSignKey[] | undefined;
1845
+ let resolved = false;
1846
+ callbackFacts = { ...facts };
1847
+ Object.defineProperty(callbackFacts, "entryPublicKeys", {
1848
+ configurable: true,
1849
+ enumerable: true,
1850
+ get: () => {
1851
+ if (!resolved) {
1852
+ callbackKeys = detachPublicKeysForCallback(sourceKeys);
1853
+ resolved = true;
1854
+ }
1855
+ return callbackKeys;
1856
+ },
1857
+ set: (value: readonly PublicSignKey[] | undefined) => {
1858
+ callbackKeys = value;
1859
+ resolved = true;
1860
+ },
1861
+ });
1862
+ }
1863
+ return this.transformer(value, context, callbackFacts);
1864
+ }
1865
+
1830
1866
  private prepareNativeBackboneDocumentIndexCommit(
1831
1867
  value: T,
1832
1868
  encodedDocument: Uint8Array,
@@ -1851,7 +1887,7 @@ export class DocumentIndex<
1851
1887
  },
1852
1888
  getIndexable: () => {
1853
1889
  if (!hasCached) {
1854
- const transformed = this.transformer(
1890
+ const transformed = this.transformWithDetachedFacts(
1855
1891
  value,
1856
1892
  projectionContext as types.Context,
1857
1893
  transformFacts,
@@ -1878,7 +1914,7 @@ export class DocumentIndex<
1878
1914
  ) {
1879
1915
  return;
1880
1916
  }
1881
- const transformed = this.transformer(
1917
+ const transformed = this.transformWithDetachedFacts(
1882
1918
  value,
1883
1919
  undefined as unknown as types.Context,
1884
1920
  transformFacts,
@@ -1924,7 +1960,7 @@ export class DocumentIndex<
1924
1960
  },
1925
1961
  getIndexable: () => {
1926
1962
  if (!hasCached) {
1927
- const transformed = this.transformer(
1963
+ const transformed = this.transformWithDetachedFacts(
1928
1964
  value,
1929
1965
  projectionContext as types.Context,
1930
1966
  transformFacts,
@@ -1961,7 +1997,11 @@ export class DocumentIndex<
1961
1997
  ) {
1962
1998
  return;
1963
1999
  }
1964
- const transformed = this.transformer(value, context, transformFacts);
2000
+ const transformed = this.transformWithDetachedFacts(
2001
+ value,
2002
+ context,
2003
+ transformFacts,
2004
+ );
1965
2005
  if (isPromiseLike(transformed)) {
1966
2006
  return;
1967
2007
  }
@@ -3150,7 +3190,11 @@ export class DocumentIndex<
3150
3190
  this.cacheResolvedValue(idString, value);
3151
3191
  const valueToIndex = this.transformerIsIdentity
3152
3192
  ? (value as any as I)
3153
- : await this.transformer(value, context, options?.transformFacts);
3193
+ : await this.transformWithDetachedFacts(
3194
+ value,
3195
+ context,
3196
+ options?.transformFacts,
3197
+ );
3154
3198
 
3155
3199
  coerceWithIndexed(value, valueToIndex);
3156
3200
 
package/src/transform.ts CHANGED
@@ -43,6 +43,7 @@ export type DocumentTransformDescriptor =
43
43
  const NATIVE_DOCUMENT_TRANSFORM = Symbol.for(
44
44
  "@peerbit/document/native-document-transform",
45
45
  );
46
+ const builtInDocumentTransformers = new WeakSet<Function>();
46
47
 
47
48
  type DescribedDocumentTransformer<T, I> = DocumentTransformer<T, I> & {
48
49
  readonly [NATIVE_DOCUMENT_TRANSFORM]?: DocumentTransformDescriptor;
@@ -142,9 +143,15 @@ const attachDocumentTransformDescriptor = <T, I>(
142
143
  writable: false,
143
144
  configurable: false,
144
145
  });
146
+ builtInDocumentTransformers.add(fn);
145
147
  return fn;
146
148
  };
147
149
 
150
+ /** Distinguish library-created transforms from forgeable public descriptors. */
151
+ export const isBuiltInDocumentTransformer = <T, I>(
152
+ transformer: DocumentTransformer<T, I> | undefined,
153
+ ): boolean => !!transformer && builtInDocumentTransformers.has(transformer);
154
+
148
155
  export const getDocumentTransformDescriptor = <T, I>(
149
156
  transformer: DocumentTransformer<T, I> | undefined,
150
157
  ): DocumentTransformDescriptor | undefined =>