flatpack-json 9.6.4 → 9.8.0

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.
@@ -0,0 +1,122 @@
1
+ import assert from 'node:assert';
2
+ import { getFlatpackedRootIdx, getIndexesReferencedByElement, isStringTableElement } from './flatpacked.mjs';
3
+ import { StringTableBuilder } from './stringTable.mjs';
4
+ import { dataHeaderV2_0, ElementType } from './types.mjs';
5
+ const emptyElement = [];
6
+ export class FlatpackData {
7
+ flatpack;
8
+ stringTable;
9
+ rootIndex;
10
+ used = new Set();
11
+ ownedBy = new Map();
12
+ available = [];
13
+ constructor(flatpack) {
14
+ this.flatpack = flatpack ? [...flatpack] : [dataHeaderV2_0, [ElementType.StringTable]];
15
+ assert(isStringTableElement(this.flatpack[1]), 'Expected a string table element at index 1 of the flatpack data');
16
+ this.stringTable = new StringTableBuilder(this.flatpack[1]);
17
+ this.rootIndex = getFlatpackedRootIdx(this.flatpack);
18
+ this.#calcAvailableIndexes();
19
+ this.available.push(this.rootIndex);
20
+ }
21
+ add(element) {
22
+ const idx = this.#getNextAvailableIndex();
23
+ this.flatpack[idx] = element;
24
+ this.used.delete(idx);
25
+ this.markUsed(idx);
26
+ return idx;
27
+ }
28
+ reserve() {
29
+ const idx = this.add(emptyElement);
30
+ return idx;
31
+ }
32
+ set(idx, element) {
33
+ this.flatpack[idx] = element;
34
+ this.used.delete(idx);
35
+ this.markUsed(idx);
36
+ }
37
+ get(idx) {
38
+ return this.flatpack[idx];
39
+ }
40
+ markUsed(idx) {
41
+ if (this.used.has(idx))
42
+ return;
43
+ this.used.add(idx);
44
+ const children = getIndexesReferencedByElement(this.flatpack[idx]);
45
+ // First claim ownership of children before recursively marking them as used.
46
+ // We need to ensure they are owned at the top.
47
+ for (const childIdx of children) {
48
+ this.claimOwnership(childIdx, idx);
49
+ }
50
+ for (const childIdx of children) {
51
+ this.markUsed(childIdx);
52
+ }
53
+ }
54
+ isUsed(idx) {
55
+ return this.used.has(idx);
56
+ }
57
+ duplicateIndex(idx) {
58
+ const element = this.get(idx);
59
+ return this.add(element);
60
+ }
61
+ delete(idx) {
62
+ this.used.delete(idx);
63
+ this.available.push(idx);
64
+ this.flatpack[idx] = emptyElement;
65
+ }
66
+ claimOwnership(idx, ownerIdx) {
67
+ const currentOwner = this.ownedBy.get(idx);
68
+ if (currentOwner !== undefined)
69
+ return currentOwner;
70
+ this.ownedBy.set(idx, ownerIdx);
71
+ return ownerIdx;
72
+ }
73
+ #getNextAvailableIndex() {
74
+ for (let idx = this.available.pop(); idx !== undefined; idx = this.available.pop()) {
75
+ if (!this.used.has(idx)) {
76
+ return idx;
77
+ }
78
+ }
79
+ return this.flatpack.length;
80
+ }
81
+ /**
82
+ * Calculate available indexes based on the current flatpack data.
83
+ * Any empty array is used as a placeholder for an available flatpack element.
84
+ */
85
+ #calcAvailableIndexes() {
86
+ const stop = this.rootIndex;
87
+ const data = this.flatpack;
88
+ for (let i = data.length - 1; i >= stop; i--) {
89
+ const elem = data[i];
90
+ if (elem === undefined || (Array.isArray(elem) && !elem.length)) {
91
+ this.available.push(i);
92
+ this.used.delete(i);
93
+ continue;
94
+ }
95
+ }
96
+ }
97
+ markUnusedAsAvailable() {
98
+ const available = new Set(this.available);
99
+ const stop = this.rootIndex;
100
+ const data = this.flatpack;
101
+ const empty = emptyElement;
102
+ for (let i = data.length - 1; i > stop; i--) {
103
+ if (this.used.has(i))
104
+ continue;
105
+ data[i] = empty;
106
+ available.add(i);
107
+ }
108
+ this.available = [...available];
109
+ }
110
+ finalize() {
111
+ this.flatpack[1] = this.stringTable.clearUnusedEntries().build();
112
+ this.#calcAvailableIndexes();
113
+ this.markUnusedAsAvailable();
114
+ let idx = this.flatpack.length - 1;
115
+ while (idx >= this.rootIndex && !this.used.has(idx)) {
116
+ --idx;
117
+ }
118
+ this.flatpack.length = idx + 1;
119
+ return this.flatpack;
120
+ }
121
+ }
122
+ //# sourceMappingURL=FlatpackData.mjs.map
@@ -0,0 +1,100 @@
1
+ import { FlatpackedWrapper } from './flatpackUtil.mjs';
2
+ import type { FlatpackApi, Flatpacked, FlatpackOptions, Serializable, Unpacked } from './types.mjs';
3
+ export declare class FlatpackStoreV1 implements FlatpackApi {
4
+ #private;
5
+ readonly options?: FlatpackOptions | undefined;
6
+ private knownElements;
7
+ private assignedElements;
8
+ private elements;
9
+ private root;
10
+ private dedupe;
11
+ private sortKeys;
12
+ private emptyObjIdx;
13
+ private ids;
14
+ /**
15
+ * Cache of primitives and objects that have been added to the data.
16
+ */
17
+ private cache;
18
+ /**
19
+ * Set of elements that have been referenced by other indexes.
20
+ */
21
+ private referenced;
22
+ /**
23
+ * Cache of arrays that have been deduped.
24
+ * The key is a hash of the array elements as a function of the index of the element.
25
+ */
26
+ private cachedArrays;
27
+ private cachedObjects;
28
+ private cachedSets;
29
+ private cachedMaps;
30
+ private cachedProxies;
31
+ /**
32
+ * Cache of strings that have been deduped and stored in the data array.
33
+ */
34
+ private knownStrings;
35
+ /**
36
+ * Cache of reversed strings that have been deduped and stored in the data array.
37
+ * This is used to find matching suffixes.
38
+ */
39
+ private knownStringsRev;
40
+ private refUndefined;
41
+ constructor(value: Serializable | FlatpackedWrapper, options?: FlatpackOptions | undefined);
42
+ setValue(value: Serializable): void;
43
+ private nextId;
44
+ private addElement;
45
+ private addValueAndElement;
46
+ private primitiveToRef;
47
+ private createSubStringRef;
48
+ private addKnownString;
49
+ private addStringPrimitive;
50
+ private addStringElement;
51
+ private stringPrefix;
52
+ private stringSuffix;
53
+ private stringToRef;
54
+ private cvtSetToRef;
55
+ private dedupeSetRefs;
56
+ private proxySetRef;
57
+ private createUniqueKeys;
58
+ private cvtMapToRef;
59
+ private dedupeMapRefs;
60
+ private proxyMapRef;
61
+ private cvtRegExpToRef;
62
+ private cvtDateToRef;
63
+ private proxyDateRef;
64
+ private cvtBigintToRef;
65
+ private cvtObjToRef;
66
+ private dedupeObject;
67
+ private proxyObjectRef;
68
+ private proxyObjectWrapperRef;
69
+ /**
70
+ *
71
+ * @param value - The array converted to an ArrayRefElement.
72
+ * @param element - the element to dedupe.
73
+ * @param cacheValue - Whether to cache the value. It is false when it is a dynamic array, like object keys,
74
+ * in that case, we want to dedupe the keys and values.
75
+ * @returns the element to use.
76
+ */
77
+ private dedupeArray;
78
+ /**
79
+ * Convert an array to an index.
80
+ * @param value - The array to convert to an index.
81
+ * @param cacheValue - Whether to cache the value.
82
+ * @returns the index of the array.
83
+ */
84
+ private arrToRef;
85
+ private proxyArrayRef;
86
+ private valueToRef;
87
+ /**
88
+ * Reset things in a way that allows for reuse.
89
+ */
90
+ private softReset;
91
+ toJSON(): Flatpacked;
92
+ static fromJSON(data: Flatpacked): FlatpackStoreV1;
93
+ static parse(content: string): FlatpackStoreV1;
94
+ stringify(): string;
95
+ toValue(): Unpacked;
96
+ _toValueProxy(): Unpacked;
97
+ }
98
+ export declare function toJSON<V extends Serializable>(json: V, options?: FlatpackOptions): Flatpacked;
99
+ export declare function stringify(data: Unpacked, pretty?: boolean): string;
100
+ //# sourceMappingURL=FlatpackV1.d.mts.map