• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

live-input-vector-output-node / livon-ts / 23789194950

31 Mar 2026 08:56AM UTC coverage: 74.971% (-8.3%) from 83.221%
23789194950

push

github

cr15p1
chore(release): bump prerelease to 0.29.0-rc.11 via changeset

947 of 1238 branches covered (76.49%)

Branch coverage included in aggregate %.

4235 of 5674 relevant lines covered (74.64%)

25.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.33
/packages/sync/src/utils/serializedKeyCache.ts
1
import { serializeKey } from './serializeKey.js';
2

3
export interface CreateSerializedKeyCacheConfig {
4
  mode?: SerializedKeyCacheMode;
5
  limit?: number;
6
  cacheObjects?: boolean;
7
  cachePrimitives?: boolean;
8
}
9

10
export interface SerializedKeyCache {
11
  getOrCreateKey: (input: unknown) => string;
12
  clear: () => void;
13
}
14

15
export type SerializedKeyCacheMode = 'identity-unit' | 'payload-hot-path' | 'dependency';
16

17
interface SerializedKeyCacheModeDefaults {
18
  cacheObjects: boolean;
19
  cachePrimitives: boolean;
20
}
21

22
const isObjectInput = (input: unknown): input is object => {
20✔
23
  return input !== null && typeof input === 'object';
1,231✔
24
};
25

26
const resolveModeDefaults = (mode: SerializedKeyCacheMode): SerializedKeyCacheModeDefaults => {
20✔
27
  if (mode === 'payload-hot-path') {
172✔
28
    return {
43✔
29
      cacheObjects: false,
30
      cachePrimitives: true,
31
    };
32
  }
33

34
  if (mode === 'dependency') {
129✔
35
    return {
10✔
36
      cacheObjects: false,
37
      cachePrimitives: false,
38
    };
39
  }
40

41
  return {
119✔
42
    cacheObjects: true,
43
    cachePrimitives: true,
44
  };
45
};
46

47
export const createSerializedKeyCache = ({
20✔
48
  mode = 'identity-unit',
49
  limit,
50
  cacheObjects,
51
  cachePrimitives,
52
}: CreateSerializedKeyCacheConfig = {}): SerializedKeyCache => {
53
  const modeDefaults = resolveModeDefaults(mode);
172✔
54
  const shouldCacheObjects = cacheObjects ?? modeDefaults.cacheObjects;
172✔
55
  const shouldCachePrimitives = cachePrimitives ?? modeDefaults.cachePrimitives;
172✔
56
  const keyByPrimitiveInput = new Map<unknown, string>();
172✔
57
  const primitiveInputOrder = new Set<unknown>();
172✔
58
  const keyByObjectInput = new WeakMap<object, string>();
172✔
59
  const isLimitEnabled = limit !== undefined && limit >= 0;
172✔
60

61
  const getOrCreateKey = (input: unknown): string => {
172✔
62
    if (isObjectInput(input)) {
1,231✔
63
      if (!shouldCacheObjects) {
1,228✔
64
        return serializeKey(input);
45✔
65
      }
66

67
      const existingObjectKey = keyByObjectInput.get(input);
1,183✔
68
      if (existingObjectKey !== undefined) {
1,183✔
69
        return existingObjectKey;
15✔
70
      }
71

72
      const createdObjectKey = serializeKey(input);
1,168✔
73
      keyByObjectInput.set(input, createdObjectKey);
1,168✔
74
      return createdObjectKey;
1,168✔
75
    }
76

77
    if (!shouldCachePrimitives) {
3✔
78
      return serializeKey(input);
×
79
    }
80

81
    const existingPrimitiveKey = keyByPrimitiveInput.get(input);
3✔
82
    if (existingPrimitiveKey !== undefined) {
3✔
83
      primitiveInputOrder.delete(input);
×
84
      primitiveInputOrder.add(input);
×
85
      return existingPrimitiveKey;
×
86
    }
87

88
    const createdPrimitiveKey = serializeKey(input);
3✔
89
    keyByPrimitiveInput.set(input, createdPrimitiveKey);
3✔
90
    primitiveInputOrder.add(input);
3✔
91

92
    if (isLimitEnabled && keyByPrimitiveInput.size > limit) {
3✔
93
      const oldestPrimitiveInput = primitiveInputOrder.values().next().value;
2✔
94
      if (oldestPrimitiveInput !== undefined) {
2✔
95
        primitiveInputOrder.delete(oldestPrimitiveInput);
2✔
96
        keyByPrimitiveInput.delete(oldestPrimitiveInput);
2✔
97
      }
98
    }
99

100
    return createdPrimitiveKey;
3✔
101
  };
102

103
  const clear = (): void => {
172✔
104
    keyByPrimitiveInput.clear();
1✔
105
    primitiveInputOrder.clear();
1✔
106
  };
107

108
  return {
172✔
109
    getOrCreateKey,
110
    clear,
111
  };
112
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc