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

overlookmotel / livepack / 7232633237

16 Dec 2023 03:00PM UTC coverage: 90.424% (-0.004%) from 90.428%
7232633237

push

github

overlookmotel
TODO

4761 of 5127 branches covered (0.0%)

Branch coverage included in aggregate %.

12850 of 14349 relevant lines covered (89.55%)

8940.81 hits per line

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

88.42
/lib/init/weak.js
1
/* --------------------
64✔
2
 * livepack module
64✔
3
 * Shim `WeakMap` + `WeakSet` to capture entries
64✔
4
 * ------------------*/
64✔
5

64✔
6
/* global WeakRef, FinalizationRegistry */
64✔
7

64✔
8
'use strict';
64✔
9

64✔
10
// Exports
64✔
11

64✔
12
module.exports = {getWeakSets, getWeakMaps};
64✔
13

64✔
14
const WeakMapOriginal = WeakMap;
64✔
15

64✔
16
/**
64✔
17
 * Shim `WeakSet` to record all `WeakSet`s created in program.
64✔
18
 * The shimmed `WeakSet` class captures entries, so they can be serialized.
64✔
19
 * Implementation uses `WeakRef` to avoid holding strong references to entries,
64✔
20
 * and allows them to be garbage collected.
64✔
21
 * Adapted from https://github.com/Lunuy/iterable-weak/blob/master/src/IterableWeakSet.ts
64✔
22
 *
64✔
23
 * @returns {WeakMap} - `WeakMap` of all `WeakSet`s.
64✔
24
 *   `WeakMap` returned maps `WeakSet`s to a `Set` of `WeakRef`s to the `WeakSet`'s entries.
64✔
25
 */
64✔
26
function getWeakSets() {
64✔
27
        const weakSets = new WeakMapOriginal();
64✔
28

64✔
29
        WeakSet = class WeakSet { // eslint-disable-line no-global-assign
64✔
30
                // Set of `WeakRefs` to entries
828✔
31
                #refs = new Set();
828✔
32
                // Mapping from entries to `WeakRef`s to those entries
828✔
33
                #mapping = new WeakMapOriginal();
828✔
34
                // FinalizationRegistry to delete `WeakRef`s from `#refs` when entries are garbage collected
828✔
35
                #finalizationRegistry = new FinalizationRegistry(ref => this.#refs.delete(ref));
828✔
36

828✔
37
                constructor(iterable = undefined) { // `= undefined` so `WeakSet.length === 0`
828✔
38
                        weakSets.set(this, this.#refs);
828✔
39

828✔
40
                        if (iterable) {
828✔
41
                                for (const element of iterable) {
24✔
42
                                        // NB: Original calls `WeakSet.prototype.add` even if it's been overwritten by user
40✔
43
                                        this.add(element);
40✔
44
                                }
40✔
45
                        }
24✔
46
                }
828✔
47

828✔
48
                delete(element) {
828✔
49
                        const ref = this.#mapping.get(element);
×
50
                        if (!ref) return false;
×
51

×
52
                        this.#mapping.delete(element);
×
53
                        this.#refs.delete(ref);
×
54
                        this.#finalizationRegistry.unregister(ref);
×
55
                        return true;
×
56
                }
×
57

828✔
58
                has(element) {
828✔
59
                        return this.#mapping.has(element);
23,352✔
60
                }
23,352✔
61

828✔
62
                add(element) {
828✔
63
                        if (!this.#mapping.has(element)) {
848✔
64
                                const ref = new WeakRef(element);
848✔
65
                                this.#mapping.set(element, ref);
848✔
66
                                this.#refs.add(ref);
848✔
67
                                this.#finalizationRegistry.register(element, ref, ref);
848✔
68
                        }
848✔
69
                        return this;
848✔
70
                }
848✔
71
        };
64✔
72

64✔
73
        // eslint-disable-next-line no-extend-native
64✔
74
        Object.defineProperty(WeakSet.prototype, Symbol.toStringTag, {value: 'WeakSet', configurable: true});
64✔
75

64✔
76
        return weakSets;
64✔
77
}
64✔
78

64✔
79
/**
64✔
80
 * Shim `WeakMap` to record all `WeakMap`s created in program.
64✔
81
 * The shimmed `WeakMap` class captures `WeakMap` keys, so they can be serialized.
64✔
82
 * Implementation uses `WeakRef` to avoid holding strong references to keys,
64✔
83
 * and allows them to be garbage collected.
64✔
84
 *
64✔
85
 * Adapted from https://github.com/tc39/proposal-weakrefs#iterable-weakmaps
64✔
86
 * and https://github.com/Lunuy/iterable-weak/blob/master/src/IterableWeakMap.ts
64✔
87
 *
64✔
88
 * @returns {WeakMap} - `WeakMap` of all `WeakMap`s.
64✔
89
 *   `WeakMap` returned maps `WeakMap`s to objects with properties `.refs` and `.mappings`.
64✔
90
 *   `.refs` is a `Set` of `WeakRef`s to the `WeakMap`'s keys.
64✔
91
 *   `.mappings` is a `WeakMap` of keys to values.
64✔
92
 */
64✔
93
function getWeakMaps() {
64✔
94
        const weakMaps = new WeakMapOriginal();
64✔
95

64✔
96
        WeakMap = class WeakMap { // eslint-disable-line no-global-assign
64✔
97
                // Set of `WeakRefs` to keys
128✔
98
                #refs = new Set();
128✔
99
                // Mapping from keys to objects containing `WeakRef` to key, and value
128✔
100
                #mapping = new WeakMapOriginal();
128✔
101
                // FinalizationRegistry to delete `WeakRef`s from `#refs` when entries are garbage collected
128✔
102
                #finalizationRegistry = new FinalizationRegistry(ref => this.#refs.delete(ref));
128✔
103

128✔
104
                constructor(iterable = undefined) { // `= undefined` so `WeakMap.length === 0`
128✔
105
                        weakMaps.set(this, {refs: this.#refs, mapping: this.#mapping});
128✔
106

128✔
107
                        if (iterable) {
128✔
108
                                for (const [key, value] of iterable) {
24✔
109
                                        // NB: Original calls `WeakMap.prototype.set` even if it's been overwritten by user
40✔
110
                                        this.set(key, value);
40✔
111
                                }
40✔
112
                        }
24✔
113
                }
128✔
114

128✔
115
                delete(key) {
128✔
116
                        const entry = this.#mapping.get(key);
×
117
                        if (!entry) return false;
×
118

×
119
                        const {ref} = entry;
×
120
                        this.#refs.delete(ref);
×
121
                        this.#mapping.delete(key);
×
122
                        this.#finalizationRegistry.unregister(ref);
×
123

×
124
                        return true;
×
125
                }
×
126

128✔
127
                get(key) {
128✔
128
                        return this.#mapping.get(key)?.value;
1,096✔
129
                }
1,096✔
130

128✔
131
                set(key, value) {
128✔
132
                        const entry = this.#mapping.get(key);
666✔
133
                        if (!entry) {
666✔
134
                                const ref = new WeakRef(key);
666✔
135
                                this.#mapping.set(key, {ref, value});
666✔
136
                                this.#refs.add(ref);
666✔
137
                                this.#finalizationRegistry.register(key, ref, ref);
666✔
138
                        } else if (entry.value !== value) {
666!
139
                                entry.value = value;
×
140
                        }
×
141

666✔
142
                        return this;
666✔
143
                }
666✔
144

128✔
145
                has(key) {
128✔
146
                        return this.#mapping.has(key);
22,476✔
147
                }
22,476✔
148
        };
64✔
149

64✔
150
        // eslint-disable-next-line no-extend-native
64✔
151
        Object.defineProperty(WeakMap.prototype, Symbol.toStringTag, {value: 'WeakMap', configurable: true});
64✔
152

64✔
153
        return weakMaps;
64✔
154
}
64✔
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