jsonschema-key-compression 1.6.1 → 2.0.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.
Files changed (49) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +65 -5
  3. package/dist/es/compress.d.ts +3 -1
  4. package/dist/es/compress.js +164 -26
  5. package/dist/es/compress.js.map +1 -1
  6. package/dist/es/create-compressed-json-schema.d.ts +9 -0
  7. package/dist/es/create-compressed-json-schema.js +136 -26
  8. package/dist/es/create-compressed-json-schema.js.map +1 -1
  9. package/dist/es/create-compression-table.d.ts +7 -1
  10. package/dist/es/create-compression-table.js +56 -30
  11. package/dist/es/create-compression-table.js.map +1 -1
  12. package/dist/es/decompress.js +41 -21
  13. package/dist/es/decompress.js.map +1 -1
  14. package/dist/es/enum-compression.d.ts +35 -0
  15. package/dist/es/enum-compression.js +217 -0
  16. package/dist/es/enum-compression.js.map +1 -0
  17. package/dist/es/index.d.ts +1 -1
  18. package/dist/es/index.js.map +1 -1
  19. package/dist/es/types.d.ts +11 -0
  20. package/dist/es/util.d.ts +1 -1
  21. package/dist/es/util.js.map +1 -1
  22. package/dist/lib/compress.d.ts +3 -1
  23. package/dist/lib/compress.js +170 -33
  24. package/dist/lib/compress.js.map +1 -1
  25. package/dist/lib/create-compressed-json-schema.d.ts +9 -0
  26. package/dist/lib/create-compressed-json-schema.js +136 -27
  27. package/dist/lib/create-compressed-json-schema.js.map +1 -1
  28. package/dist/lib/create-compression-table.d.ts +7 -1
  29. package/dist/lib/create-compression-table.js +61 -35
  30. package/dist/lib/create-compression-table.js.map +1 -1
  31. package/dist/lib/decompress.js +44 -25
  32. package/dist/lib/decompress.js.map +1 -1
  33. package/dist/lib/enum-compression.d.ts +35 -0
  34. package/dist/lib/enum-compression.js +223 -0
  35. package/dist/lib/enum-compression.js.map +1 -0
  36. package/dist/lib/index.d.ts +1 -1
  37. package/dist/lib/index.js.map +1 -1
  38. package/dist/lib/types.d.ts +11 -0
  39. package/dist/lib/util.d.ts +1 -1
  40. package/dist/lib/util.js +3 -3
  41. package/dist/lib/util.js.map +1 -1
  42. package/package.json +14 -12
  43. package/src/compress.ts +184 -34
  44. package/src/create-compressed-json-schema.ts +158 -35
  45. package/src/create-compression-table.ts +59 -31
  46. package/src/decompress.ts +42 -27
  47. package/src/enum-compression.ts +240 -0
  48. package/src/index.ts +1 -0
  49. package/src/types.d.ts +11 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ ## Unreleased
4
+
5
+ - FIX: `createCompressedJsonSchema()` did not compress the `properties`, `required` and `items` of nested schemas whose `type` is a type-array like `['object', 'null']` or `['array', 'null']`, or whose `type` is omitted. Because `createCompressionTable()` and `compressObject()` do compress these keys, a compressed object did not validate against its compressed schema.
6
+ - FIX: `createCompressedJsonSchema()` did not compress property names inside of `allOf`, `anyOf`, `oneOf`, `not`, `patternProperties`, `additionalItems`, `dependencies` and `definitions`. The compression-table is not changed by this, so already compressed data stays compatible.
7
+ - FIX: `createCompressionTable()` with `ignoreProperties` removed the decompression entry of a different property when an ignored property name was equal to a generated compressed key like `b`. `decompressObject()` then left that key compressed.
8
+ - FIX: `createCompressedJsonSchema()` did not compress object values inside of `enum`. A property with an enum of allowed objects rejected every compressed document, because the enum still listed the uncompressed property names.
9
+ - FIX: The performance test measured the creation of the random test-data (and for decompress also the compression) as part of the compress and decompress timings, which reported about 2 to 3 times too high numbers. The timer now only covers the measured operation, after an untimed warm-up. The benchmark schema also describes `shoppingCartItems` as an array now, like the test-data has it.
10
+ - PERFORMANCE: `compressObject()` is about 60% faster and `decompressObject()` about 50% faster. Keys are looked up directly instead of being split on `[` for every key, the flagged compressed keys are cached per compression-table instead of being concatenated on every use, and primitive values are no longer passed through a recursive call.
11
+ - PERFORMANCE: `createCompressionTable()` is about 30% faster because the property names of nested schemas are collected into a single set instead of creating a new set and array copy on each nesting level.
12
+ - ADD: Enum-compression. When it is enabled, the value of a property is stored as the index of that value inside of the `enum` of the property. It is opt-in with the fourth parameter of `createCompressionTable()` so that data which was compressed without it stays readable. Only unambiguous string-enums are compressed, and the indexes are assigned in the alphabetical order of the values so that sorting and range-queries keep working. `compressQuery()` transforms the compared values and resolves a `$regex` into the matching indexes.
13
+ - ADD: A general consistency test that runs random mango-queries (via mingo) and schema validation (via ajv) on random documents of a complex schema, to ensure that compressed and non-compressed data behave the same.
package/README.md CHANGED
@@ -62,15 +62,18 @@ The efficiency depends on the amount and length of the attribute names.
62
62
  * Just using gzip on the json would result in **180 chars**
63
63
  * Using gzip+key-compression ends in a string with only **127 chars**
64
64
 
65
+ The [enum-compression](#enum-compression) saves additional space,
66
+ the longer the values of the enums are the more it saves.
67
+
65
68
  You can reproduce these results by running `npm run test:efficiency`.
66
69
 
67
70
  ## Performance
68
71
 
69
- The compression works pretty fast. Here are some time measurements on a single intel i7 CPU.
72
+ The compression works pretty fast. Here are some time measurements on a single CPU core of a Node.js 22 process. Only the measured operation is timed, the creation of the test-data is not part of the measurement.
70
73
 
71
- * Creating a compression-table from the schema of the object above takes about `0.02ms`
72
- * Compressing the example-object from above takes about `0.021ms`
73
- * Decompressing takes about `0.027ms` per object
74
+ * Creating a compression-table from the schema of the object above takes about `0.005ms`
75
+ * Compressing the example-object from above takes about `0.0025ms`
76
+ * Decompressing takes about `0.002ms` per object
74
77
 
75
78
  You can reproduce these results by running `npm run test:performance`.
76
79
 
@@ -103,11 +106,64 @@ Creates a compression-table from the [json-schema](https://json-schema.org/).
103
106
 
104
107
  ```js
105
108
  import {
106
- createCompressionTable
109
+ createCompressionTable,
110
+ DEFAULT_COMPRESSION_FLAG
107
111
  } from 'jsonschema-key-compression';
108
112
  const compressionTable = createCompressionTable(jsonSchema);
113
+
114
+ // all parameters
115
+ const compressionTable = createCompressionTable(
116
+ jsonSchema,
117
+ DEFAULT_COMPRESSION_FLAG, // the character that marks a compressed key
118
+ ['myProperty'], // property-names that must not be compressed
119
+ true // enable the enum-compression, see below
120
+ );
121
+ ```
122
+
123
+ ### Enum-compression
124
+
125
+ Enum-compression stores the value of a property as the index of that value
126
+ inside of the `enum` of the property. With the enum `['admin', 'editor', 'viewer']`
127
+ the value `'editor'` is stored as the number `1`.
128
+
129
+ It is opt-in with the fourth parameter of `createCompressionTable()`,
130
+ so that data which was compressed without it stays readable.
131
+
132
+ ```js
133
+ const compressionTable = createCompressionTable(
134
+ jsonSchema,
135
+ DEFAULT_COMPRESSION_FLAG,
136
+ [],
137
+ true
138
+ );
139
+
140
+ compressObject(compressionTable, {
141
+ userRole: 'editor',
142
+ colorTags: ['red', 'blue']
143
+ });
144
+ // > { '|b': 1, '|a': [2, 0] }
109
145
  ```
110
146
 
147
+ A property is only enum-compressed when it is safe to do so:
148
+ * All values of the enum must be strings. A number inside of the enum could not be
149
+ told apart from an index, and strings are where the saved space is.
150
+ * The property name must have the same enum everywhere in the schema.
151
+ A name that is used with different enums, or that has no enum somewhere else,
152
+ is left alone because the same number would otherwise mean different things.
153
+ * Properties in `ignoreProperties` are not enum-compressed.
154
+
155
+ The enum-values are sorted before the indexes are assigned, so the order of the indexes
156
+ is the alphabetical order of the values. Because of this, `sort` and the range-operators
157
+ `$gt`, `$gte`, `$lt` and `$lte` keep working on compressed data.
158
+ `compressQuery()` also transforms the values of `$eq`, `$ne`, `$in`, `$nin`, `$all`,
159
+ `$not` and `$elemMatch`, and resolves a `$regex` into the `$in` of the matching indexes.
160
+ The operators `$type` and `$mod` are not transformed, they would run on the
161
+ number of the compressed value.
162
+
163
+ Enum-compression assumes that the data matches the schema.
164
+ A document that stores a number where the schema defines an enum of strings
165
+ cannot be told apart from an already compressed value.
166
+
111
167
  ### compressObject
112
168
  Compress a json-object based on its schema.
113
169
 
@@ -232,3 +288,7 @@ console.dir(compressedSchema);
232
288
  */
233
289
 
234
290
  ```
291
+
292
+ ## About
293
+
294
+ This module was originally created for the [RxDB compression plugin](https://rxdb.info/key-compression.html) but in theory it can be used in any json based state management system or database.
@@ -22,5 +22,7 @@ export declare function compressedAndFlaggedKey(table: CompressionTable, key: st
22
22
  export declare function compressQuery(table: CompressionTable, query: MangoQuery): MangoQuery;
23
23
  /**
24
24
  * @recursive
25
+ * @param enumValues The enum of the property that the selector belongs to,
26
+ * so that the compared values can be enum-compressed.
25
27
  */
26
- export declare function compressQuerySelector(table: CompressionTable, selector: any): any;
28
+ export declare function compressQuerySelector(table: CompressionTable, selector: any, enumValues?: string[]): any;
@@ -1,3 +1,4 @@
1
+ import { compressEnumValue } from './enum-compression';
1
2
  /**
2
3
  * compress the keys of an object via the compression-table
3
4
  * @recursive
@@ -5,21 +6,35 @@
5
6
  export function compressObject(table, obj) {
6
7
  if (typeof obj !== 'object' || obj === null)
7
8
  return obj;
8
- else if (Array.isArray(obj)) {
9
+ if (Array.isArray(obj)) {
9
10
  // array
10
- return obj
11
- .map(function (item) { return compressObject(table, item); });
11
+ var retArray = new Array(obj.length);
12
+ for (var index = 0; index < obj.length; index++) {
13
+ var item = obj[index];
14
+ // primitives do not need a recursive call
15
+ retArray[index] = (typeof item === 'object' && item !== null) ? compressObject(table, item) : item;
16
+ }
17
+ return retArray;
12
18
  }
13
- else {
14
- // object
15
- var ret_1 = {};
16
- Object.keys(obj).forEach(function (key) {
17
- var compressedKey = compressedAndFlaggedKey(table, key);
18
- var value = compressObject(table, obj[key]);
19
- ret_1[compressedKey] = value;
20
- });
21
- return ret_1;
19
+ // object
20
+ var ret = {};
21
+ var keys = Object.keys(obj);
22
+ var enumCompression = table.enumCompression;
23
+ for (var index = 0; index < keys.length; index++) {
24
+ var key = keys[index];
25
+ var value = obj[key];
26
+ var enumValues = enumCompression && enumCompression.get(key);
27
+ if (enumValues && (typeof value === 'string' || Array.isArray(value))) {
28
+ ret[compressedAndFlaggedKey(table, key)] = compressEnumValue(enumValues, value);
29
+ }
30
+ else {
31
+ // primitives do not need a recursive call
32
+ ret[compressedAndFlaggedKey(table, key)] = (typeof value === 'object' && value !== null) ?
33
+ compressObject(table, value) :
34
+ value;
35
+ }
22
36
  }
37
+ return ret;
23
38
  }
24
39
  /**
25
40
  * transform an object-path
@@ -42,22 +57,45 @@ export function throwErrorIfCompressionFlagUsed(table, key) {
42
57
  table.compressionFlag + ' on key ' + key);
43
58
  }
44
59
  }
60
+ /**
61
+ * Cache of the flagged compressed keys per table,
62
+ * so that the flag does not have to be concatenated on each use.
63
+ * The cache is keyed by the table object, so a table must not be
64
+ * mutated after it has been used for compression.
65
+ */
66
+ var flaggedKeysCache = new WeakMap();
67
+ function getFlaggedKeys(table) {
68
+ var flaggedKeys = flaggedKeysCache.get(table);
69
+ if (!flaggedKeys) {
70
+ var newFlaggedKeys_1 = new Map();
71
+ table.compressedToUncompressed.forEach(function (compressedKey, key) {
72
+ newFlaggedKeys_1.set(key, table.compressionFlag + compressedKey);
73
+ });
74
+ flaggedKeysCache.set(table, newFlaggedKeys_1);
75
+ flaggedKeys = newFlaggedKeys_1;
76
+ }
77
+ return flaggedKeys;
78
+ }
45
79
  export function compressedAndFlaggedKey(table, key) {
46
80
  throwErrorIfCompressionFlagUsed(table, key);
47
81
  /**
48
82
  * keys could be array-accessors like myArray[4]
49
- * we have to split and readd the squared brackets value
83
+ * so the part before the squared bracket is looked up
84
+ * and the bracket part is re-added.
85
+ * Most keys have no bracket, so the plain lookup is the fast path.
50
86
  */
51
- var splitSquaredBrackets = key.split('[');
52
- key = splitSquaredBrackets.shift();
53
- var compressedKey = table.compressedToUncompressed.get(key);
54
- if (!compressedKey) {
55
- return key;
87
+ var flaggedKeys = getFlaggedKeys(table);
88
+ var bracketIndex = key.indexOf('[');
89
+ if (bracketIndex === -1) {
90
+ var directFlaggedKey = flaggedKeys.get(key);
91
+ return directFlaggedKey ? directFlaggedKey : key;
56
92
  }
57
- else {
58
- var readdSquared = splitSquaredBrackets.length ? '[' + splitSquaredBrackets.join('[') : '';
59
- return table.compressionFlag + compressedKey + readdSquared;
93
+ var plainKey = key.slice(0, bracketIndex);
94
+ var flaggedKey = flaggedKeys.get(plainKey);
95
+ if (!flaggedKey) {
96
+ return key;
60
97
  }
98
+ return flaggedKey + key.slice(bracketIndex);
61
99
  }
62
100
  /**
63
101
  * compress a mango-query
@@ -108,33 +146,133 @@ export function compressQuery(table, query) {
108
146
  }
109
147
  return ret;
110
148
  }
149
+ /**
150
+ * Operators whose value is a document-value and therefore
151
+ * has to be enum-compressed like the documents themselves.
152
+ * Any other operator, like $type or $mod, gets its value unchanged.
153
+ */
154
+ var ENUM_VALUE_OPERATORS = [
155
+ '$eq',
156
+ '$ne',
157
+ '$gt',
158
+ '$gte',
159
+ '$lt',
160
+ '$lte',
161
+ '$in',
162
+ '$nin',
163
+ '$all',
164
+ '$not',
165
+ '$elemMatch'
166
+ ];
167
+ /**
168
+ * Returns the enum of the property that the path points to,
169
+ * or undefined when that property is not enum-compressed.
170
+ */
171
+ function enumValuesOfPath(table, path) {
172
+ var enumCompression = table.enumCompression;
173
+ if (!enumCompression) {
174
+ return undefined;
175
+ }
176
+ var splitted = path.split('.');
177
+ var lastKey = splitted[splitted.length - 1];
178
+ var bracketIndex = lastKey.indexOf('[');
179
+ if (bracketIndex !== -1) {
180
+ lastKey = lastKey.slice(0, bracketIndex);
181
+ }
182
+ return enumCompression.get(lastKey);
183
+ }
184
+ /**
185
+ * A regular expression cannot run on the compressed number of an enum-value,
186
+ * so it is resolved up front into the indexes of the matching enum-values.
187
+ */
188
+ function enumIndexesOfRegex(enumValues, pattern, options) {
189
+ var source;
190
+ var flags;
191
+ if (pattern instanceof RegExp) {
192
+ source = pattern.source;
193
+ flags = (pattern.ignoreCase ? 'i' : '') + (pattern.multiline ? 'm' : '');
194
+ }
195
+ else {
196
+ source = String(pattern);
197
+ flags = '';
198
+ }
199
+ if (typeof options === 'string') {
200
+ flags = options;
201
+ }
202
+ /**
203
+ * The global flags make .test() stateful
204
+ * and are meaningless for a full check of a single value.
205
+ */
206
+ flags = flags.replace(/[gy]/g, '');
207
+ var regex = new RegExp(source, flags);
208
+ var indexes = [];
209
+ for (var i = 0; i < enumValues.length; i++) {
210
+ if (regex.test(enumValues[i])) {
211
+ indexes.push(i);
212
+ }
213
+ }
214
+ return indexes;
215
+ }
111
216
  /**
112
217
  * @recursive
218
+ * @param enumValues The enum of the property that the selector belongs to,
219
+ * so that the compared values can be enum-compressed.
113
220
  */
114
- export function compressQuerySelector(table, selector) {
221
+ export function compressQuerySelector(table, selector, enumValues) {
115
222
  if (Array.isArray(selector)) {
116
- return selector.map(function (item) { return compressQuerySelector(table, item); });
223
+ return selector.map(function (item) { return compressQuerySelector(table, item, enumValues); });
117
224
  }
118
225
  else if (selector instanceof RegExp) {
119
226
  return selector;
120
227
  }
121
228
  else if (typeof selector === 'object' && selector !== null) {
122
- var ret_2 = {};
229
+ var ret_1 = {};
230
+ /**
231
+ * $regex and its $options are replaced by a single $in
232
+ * with the indexes of the matching enum-values.
233
+ */
234
+ var regexIndexes_1;
235
+ if (enumValues && typeof selector['$regex'] !== 'undefined') {
236
+ regexIndexes_1 = enumIndexesOfRegex(enumValues, selector['$regex'], selector['$options']);
237
+ }
123
238
  Object.keys(selector).forEach(function (key) {
239
+ if (regexIndexes_1 && (key === '$regex' || key === '$options')) {
240
+ return;
241
+ }
124
242
  var useKey;
243
+ var useEnumValues;
125
244
  if (key.startsWith('$')) {
126
245
  // operator
127
246
  useKey = key;
247
+ useEnumValues = ENUM_VALUE_OPERATORS.indexOf(key) === -1 ? undefined : enumValues;
128
248
  }
129
249
  else {
130
250
  // property path
131
251
  useKey = compressedPath(table, key);
252
+ useEnumValues = enumValuesOfPath(table, key);
253
+ }
254
+ var value = selector[key];
255
+ if (useEnumValues && value instanceof RegExp) {
256
+ ret_1[useKey] = {
257
+ $in: enumIndexesOfRegex(useEnumValues, value, undefined)
258
+ };
259
+ }
260
+ else {
261
+ ret_1[useKey] = compressQuerySelector(table, value, useEnumValues);
132
262
  }
133
- ret_2[useKey] = compressQuerySelector(table, selector[key]);
134
263
  });
135
- return ret_2;
264
+ if (regexIndexes_1) {
265
+ var alreadyIn_1 = ret_1['$in'];
266
+ ret_1['$in'] = Array.isArray(alreadyIn_1) ?
267
+ regexIndexes_1.filter(function (index) { return alreadyIn_1.includes(index); }) :
268
+ regexIndexes_1;
269
+ }
270
+ return ret_1;
136
271
  }
137
272
  else {
273
+ if (enumValues) {
274
+ return compressEnumValue(enumValues, selector);
275
+ }
138
276
  return selector;
139
277
  }
140
278
  }
@@ -1 +1 @@
1
- {"version":3,"file":"compress.js","sourceRoot":"","sources":["../../src/compress.ts"],"names":[],"mappings":"AAMA;;;GAGG;AACH,MAAM,UAAU,cAAc,CAC1B,KAAuB,EACvB,GAAoB;IAEpB,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI;QAAE,OAAO,GAAG,CAAC;SACnD,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QACzB,QAAQ;QACR,OAAO,GAAG;aACL,GAAG,CAAC,UAAA,IAAI,IAAI,OAAA,cAAc,CAAC,KAAK,EAAE,IAAI,CAAC,EAA3B,CAA2B,CAAC,CAAC;KACjD;SAAM;QACH,SAAS;QACT,IAAM,KAAG,GAAoB,EAAE,CAAC;QAChC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,UAAA,GAAG;YACxB,IAAM,aAAa,GAAG,uBAAuB,CACzC,KAAK,EACL,GAAG,CACN,CAAC;YACF,IAAM,KAAK,GAAG,cAAc,CACxB,KAAK,EACL,GAAG,CAAC,GAAG,CAAC,CACX,CAAC;YACF,KAAG,CAAC,aAAa,CAAC,GAAG,KAAK,CAAC;QAC/B,CAAC,CAAC,CAAC;QACH,OAAO,KAAG,CAAC;KACd;AACL,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAC1B,KAAuB,EACvB,IAAY;IAEZ,IAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACjC,OAAO,QAAQ;SACV,GAAG,CAAC,UAAA,MAAM;QACP,IAAM,aA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AAG,EAAE,kBAAkB,CAAC,aAAa,EAAE,KAAK,EAAE,SAAS,CAAC;iBAC3D,CAAC;YACN,CAAC;iBAAM,CAAC;gBACJ,KAAG,CAAC,MAAM,CAAC,GAAG,qBAAqB,CAC/B,KAAK,EACL,KAAK,EACL,aAAa,CAChB,CAAC;YACN,CAAC;QACL,CAAC,CAAC,CAAC;QACH,IAAI,cAAY,EAAE,CAAC;YACf,IAAM,WAAS,GAAG,KAAG,CAAC,KAAK,CAAC,CAAC;YAC7B,KAAG,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,OAAO,CAAC,WAAS,CAAC,CAAC,CAAC;gBACnC,cAAY,CAAC,MAAM,CAAC,UAAA,KAAK,IAAI,OAAA,WAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAzB,CAAyB,CAAC,CAAC,CAAC;gBACzD,cAAY,CAAC;QACrB,CAAC;QACD,OAAO,KAAG,CAAC;IACf,CAAC;SAAM,CAAC;QACJ,IAAI,UAAU,EAAE,CAAC;YACb,OAAO,iBAAiB,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;QACnD,CAAC;QACD,OAAO,QAAQ,CAAC;IACpB,CAAC;AACL,CAAC"}
@@ -1,2 +1,11 @@
1
1
  import type { JsonSchema, CompressionTable } from './types';
2
+ /**
3
+ * Transforms the schema so that it describes the compressed objects.
4
+ * It recurses into all keywords that can contain sub-schemas,
5
+ * independent of the 'type' field. This mirrors how compressObject() works,
6
+ * which compresses every key that is known to the compression-table,
7
+ * wherever the key occurs in the document. For example a nullable nested object
8
+ * has type: ['object', 'null'] and its properties must still be compressed,
9
+ * and properties inside of oneOf/anyOf/allOf must be compressed as well.
10
+ */
2
11
  export declare function createCompressedJsonSchema(compressionTable: CompressionTable, schema: JsonSchema): JsonSchema;
@@ -1,38 +1,148 @@
1
1
  import { flatClone } from './util';
2
- import { compressedPath } from './compress';
2
+ import { compressedPath, compressObject } from './compress';
3
+ /**
4
+ * A value that is enum-compressed is stored as the index of the value,
5
+ * so the type of the property changes from string to number.
6
+ */
7
+ function toNumberType(type) {
8
+ if (!type) {
9
+ return type;
10
+ }
11
+ if (Array.isArray(type)) {
12
+ return type.map(function (single) { return single === 'string' ? 'number' : single; });
13
+ }
14
+ return type === 'string' ? 'number' : type;
15
+ }
16
+ /**
17
+ * Replaces the enum-values of an enum-compressed property
18
+ * by the indexes that the compressed documents contain.
19
+ * The enum can sit at the property itself or at the items of an array-property.
20
+ * @recursive
21
+ */
22
+ function compressEnumOfSchema(schema, enumValues) {
23
+ var ret = schema;
24
+ if (Array.isArray(schema.enum)) {
25
+ ret = flatClone(schema);
26
+ ret.enum = enumValues.map(function (_value, index) { return index; });
27
+ ret.type = toNumberType(schema.type);
28
+ }
29
+ var items = ret.items;
30
+ if (items && !Array.isArray(items) && Array.isArray(items.enum)) {
31
+ if (ret === schema) {
32
+ ret = flatClone(schema);
33
+ }
34
+ ret.items = compressEnumOfSchema(items, enumValues);
35
+ }
36
+ return ret;
37
+ }
38
+ /**
39
+ * Transforms the schema so that it describes the compressed objects.
40
+ * It recurses into all keywords that can contain sub-schemas,
41
+ * independent of the 'type' field. This mirrors how compressObject() works,
42
+ * which compresses every key that is known to the compression-table,
43
+ * wherever the key occurs in the document. For example a nullable nested object
44
+ * has type: ['object', 'null'] and its properties must still be compressed,
45
+ * and properties inside of oneOf/anyOf/allOf must be compressed as well.
46
+ */
3
47
  export function createCompressedJsonSchema(compressionTable, schema) {
4
- if (schema.type === 'array' && schema.items) {
5
- var cloned = flatClone(schema);
48
+ var hasNested = schema.items ||
49
+ schema.additionalItems ||
50
+ schema.properties ||
51
+ schema.patternProperties ||
52
+ schema.required ||
53
+ schema.allOf ||
54
+ schema.anyOf ||
55
+ schema.oneOf ||
56
+ schema.not ||
57
+ schema.dependencies ||
58
+ schema.definitions ||
59
+ schema.enum;
60
+ if (!hasNested) {
61
+ // no deeper fields in the schema
62
+ return schema;
63
+ }
64
+ var cloned = flatClone(schema);
65
+ var enumCompression = compressionTable.enumCompression;
66
+ var compressSchema = function (subSchema) { return createCompressedJsonSchema(compressionTable, subSchema); };
67
+ var compressKey = function (key) { return compressedPath(compressionTable, key); };
68
+ var compressSchemaMap = function (schemaMap, compressKeys) {
69
+ var ret = {};
70
+ // do not use Object.entries, it is transpiled shitty
71
+ Object.keys(schemaMap).forEach(function (key) {
72
+ var useKey = compressKeys ? compressKey(key) : key;
73
+ var subSchema = compressSchema(schemaMap[key]);
74
+ /**
75
+ * Only real property names can be enum-compressed,
76
+ * the keys of patternProperties and definitions are no property names.
77
+ */
78
+ var enumValues = (compressKeys && enumCompression) ? enumCompression.get(key) : undefined;
79
+ ret[useKey] = enumValues ? compressEnumOfSchema(subSchema, enumValues) : subSchema;
80
+ });
81
+ return ret;
82
+ };
83
+ if (schema.items) {
6
84
  if (Array.isArray(schema.items)) {
7
- var newItems = schema.items.map(function (item) { return createCompressedJsonSchema(compressionTable, item); });
8
- cloned.items = newItems;
85
+ cloned.items = schema.items.map(compressSchema);
9
86
  }
10
87
  else {
11
- var newItems = createCompressedJsonSchema(compressionTable, schema.items);
12
- cloned.items = newItems;
88
+ cloned.items = compressSchema(schema.items);
13
89
  }
14
- return cloned;
15
90
  }
16
- else if (schema.type === 'object' && schema.properties) {
17
- var cloned = flatClone(schema);
18
- // compress all property names
19
- var newProperties_1 = {};
20
- // do not use Object.entries, it is transpiled shitty
21
- Object.keys(schema.properties).forEach(function (key) {
22
- var property = schema.properties[key];
23
- var compressedKey = compressedPath(compressionTable, key);
24
- newProperties_1[compressedKey] = createCompressedJsonSchema(compressionTable, property);
91
+ if (schema.additionalItems && typeof schema.additionalItems === 'object') {
92
+ cloned.additionalItems = compressSchema(schema.additionalItems);
93
+ }
94
+ // compress all property names
95
+ if (schema.properties) {
96
+ cloned.properties = compressSchemaMap(schema.properties, true);
97
+ }
98
+ // the patterns are not property names, only the sub-schemas are compressed
99
+ if (schema.patternProperties) {
100
+ cloned.patternProperties = compressSchemaMap(schema.patternProperties, false);
101
+ }
102
+ // also compress the required array
103
+ if (schema.required) {
104
+ cloned.required = schema.required.map(compressKey);
105
+ }
106
+ if (schema.allOf) {
107
+ cloned.allOf = schema.allOf.map(compressSchema);
108
+ }
109
+ if (schema.anyOf) {
110
+ cloned.anyOf = schema.anyOf.map(compressSchema);
111
+ }
112
+ if (schema.oneOf) {
113
+ cloned.oneOf = schema.oneOf.map(compressSchema);
114
+ }
115
+ if (schema.not) {
116
+ cloned.not = compressSchema(schema.not);
117
+ }
118
+ /**
119
+ * dependencies are keyed by property name
120
+ * and contain either a list of property names
121
+ * or a sub-schema
122
+ */
123
+ if (schema.dependencies) {
124
+ var dependencies_1 = schema.dependencies;
125
+ var newDependencies_1 = {};
126
+ Object.keys(dependencies_1).forEach(function (key) {
127
+ var dependency = dependencies_1[key];
128
+ newDependencies_1[compressKey(key)] = Array.isArray(dependency) ?
129
+ dependency.map(compressKey) :
130
+ compressSchema(dependency);
25
131
  });
26
- cloned.properties = newProperties_1;
27
- // also compress the required array
28
- if (cloned.required) {
29
- cloned.required = cloned.required.map(function (key) { return compressedPath(compressionTable, key); });
30
- }
31
- return cloned;
132
+ cloned.dependencies = newDependencies_1;
32
133
  }
33
- else {
34
- // no deeper fields in the schema
35
- return schema;
134
+ // definition names are not property names, only the sub-schemas are compressed
135
+ if (schema.definitions) {
136
+ cloned.definitions = compressSchemaMap(schema.definitions, false);
137
+ }
138
+ /**
139
+ * enum values are document fragments,
140
+ * so object values contain property names that must be compressed.
141
+ * Primitive values are returned unchanged by compressObject().
142
+ */
143
+ if (schema.enum) {
144
+ cloned.enum = schema.enum.map(function (value) { return compressObject(compressionTable, value); });
36
145
  }
146
+ return cloned;
37
147
  }
38
148
  //# sourceMappingURL=create-compressed-json-schema.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"create-compressed-json-schema.js","sourceRoot":"","sources":["../../src/create-compressed-json-schema.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AACnC,OAAO,EAAE,c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AAI,MAAM,CAAC,QAAQ,EAAE;YACjB,MAAM,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAA,GAAG,IAAI,OAAA,cAAc,CAAC,gBAAgB,EAAE,GAAG,CAAC,EAArC,CAAqC,CAAC,CAAC;SACvF;QAED,OAAO,MAAM,CAAC;KACjB;SAAM;QACH,iCAAiC;QACjC,OAAO,MAAM,CAAC;KACjB;AACL,CAAC"}
1
+ {"version":3,"file":"create-compressed-json-schema.js","sourceRoot":"","sources":["../../src/create-compressed-json-schema.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AACnC,OAAO,EACH,cAAc,EACd,cAAc,EACjB,MAAM,YAAY,CAAC;AAEpB;;;GAGG;AACH,SAAS,YAAY,CACjB,IAAqD;IAErD,IAAI,CAAC,IAAI,EAAE,CAAC;QACR,OAAO,IAAI,CAAC;IAChB,CAAC;IACD,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QACtB,OAAO,IAAI,CAAC,GAAG,CAAC,UAAA,MAAM,IAAI,OAAA,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAvC,CAAuC,CAAC,CAAC;IACvE,CAAC;IACD,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC;AAC/C,CAAC;AAED;;;;;GAKG;AACH,SAAS,oBAAoB,CACzB,MAAkB,EAClB,UAAoB;IAEpB,IAAI,GAAG,GAAG,MAAM,CAAC;IACjB,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;QAC7B,GAAG,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC;QACxB,GAAG,CAAC,IAAI,GAAG,UAAU,CAAC,GAAG,CAAC,UAAC,MAAM,EAAE,KAAK,IAAK,OAAA,KAAK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,CAAC,UAAA,GAAG;YACjC,IAAM,UAAU,GAAG,cAAY,CAAC,GAAG,CAA0B,CAAC;YAC9D,iBAAe,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC;gBAC3D,UAAU,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC;gBAC7B,cAAc,CAAC,UAAU,CAAC,CAAC;QACnC,CAAC,CAAC,CAAC;QACH,MAAM,CAAC,YAAY,GAAG,iBAAe,CAAC;IAC1C,CAAC;IAED,+EAA+E;IAC/E,IAAI,MAAM,CAAC,WAAW,EAAE,CAAC;QACrB,MAAM,CAAC,WAAW,GAAG,iBAAiB,CAAC,MAAM,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;IACtE,CAAC;IAED;;;;OAIG;IACH,IAAI,MAAM,CAAC,IAAI,EAAE,CAAC;QACd,MAAM,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,UAAA,KAAK,IAAI,OAAA,cAAc,CAAC,gBAAgB,EAAE,KAAK,CAAC,EAAvC,CAAuC,CAAC,CAAC;IACpF,CAAC;IAED,OAAO,MAAM,CAAC;AAClB,CAAC"}
@@ -6,7 +6,13 @@ import type { CompressionTable, JsonSchema, TableType } from './types';
6
6
  * compression-flag, an error will be thrown.
7
7
  */
8
8
  export declare const DEFAULT_COMPRESSION_FLAG = "|";
9
- export declare function createCompressionTable(schema: JsonSchema, compressionFlag?: string, ignoreProperties?: string[]): CompressionTable;
9
+ /**
10
+ * @param compressEnums If true, the values of string-enums are compressed
11
+ * into the index of the value inside of the enum.
12
+ * This changes the stored format, so it is opt-in
13
+ * to keep already compressed data readable.
14
+ */
15
+ export declare function createCompressionTable(schema: JsonSchema, compressionFlag?: string, ignoreProperties?: string[], compressEnums?: boolean): CompressionTable;
10
16
  /**
11
17
  * Returns a list of all property names that occur in the schema.
12
18
  * @returns Set of strings to ensure uniqueness.