tersejson 0.1.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.
- package/LICENSE +21 -0
- package/README.md +476 -0
- package/dist/analytics.d.mts +186 -0
- package/dist/analytics.d.ts +186 -0
- package/dist/analytics.js +226 -0
- package/dist/analytics.js.map +1 -0
- package/dist/analytics.mjs +217 -0
- package/dist/analytics.mjs.map +1 -0
- package/dist/client-BQAZg7I8.d.mts +138 -0
- package/dist/client-DOOGwp_p.d.ts +138 -0
- package/dist/client.d.mts +2 -0
- package/dist/client.d.ts +2 -0
- package/dist/client.js +200 -0
- package/dist/client.js.map +1 -0
- package/dist/client.mjs +188 -0
- package/dist/client.mjs.map +1 -0
- package/dist/express-BoL__Ao6.d.mts +67 -0
- package/dist/express-LSVylWpN.d.ts +67 -0
- package/dist/express.d.mts +4 -0
- package/dist/express.d.ts +4 -0
- package/dist/express.js +522 -0
- package/dist/express.js.map +1 -0
- package/dist/express.mjs +512 -0
- package/dist/express.mjs.map +1 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +1001 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +985 -0
- package/dist/index.mjs.map +1 -0
- package/dist/integrations-7WeFO1Lk.d.mts +264 -0
- package/dist/integrations-7WeFO1Lk.d.ts +264 -0
- package/dist/integrations.d.mts +1 -0
- package/dist/integrations.d.ts +1 -0
- package/dist/integrations.js +381 -0
- package/dist/integrations.js.map +1 -0
- package/dist/integrations.mjs +367 -0
- package/dist/integrations.mjs.map +1 -0
- package/dist/types-CzaGQaV7.d.mts +134 -0
- package/dist/types-CzaGQaV7.d.ts +134 -0
- package/package.json +87 -0
package/dist/client.mjs
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// src/types.ts
|
|
2
|
+
function isTersePayload(value) {
|
|
3
|
+
return typeof value === "object" && value !== null && "__terse__" in value && value.__terse__ === true && "v" in value && "k" in value && "d" in value;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// src/core.ts
|
|
7
|
+
function expandObject(obj, shortToKey, maxDepth, currentDepth = 0) {
|
|
8
|
+
if (currentDepth >= maxDepth) return obj;
|
|
9
|
+
const expanded = {};
|
|
10
|
+
for (const [shortKey, value] of Object.entries(obj)) {
|
|
11
|
+
const originalKey = shortToKey.get(shortKey) ?? shortKey;
|
|
12
|
+
if (Array.isArray(value)) {
|
|
13
|
+
expanded[originalKey] = value.map((item) => {
|
|
14
|
+
if (typeof item === "object" && item !== null && !Array.isArray(item)) {
|
|
15
|
+
return expandObject(
|
|
16
|
+
item,
|
|
17
|
+
shortToKey,
|
|
18
|
+
maxDepth,
|
|
19
|
+
currentDepth + 1
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
return item;
|
|
23
|
+
});
|
|
24
|
+
} else if (typeof value === "object" && value !== null) {
|
|
25
|
+
expanded[originalKey] = expandObject(
|
|
26
|
+
value,
|
|
27
|
+
shortToKey,
|
|
28
|
+
maxDepth,
|
|
29
|
+
currentDepth + 1
|
|
30
|
+
);
|
|
31
|
+
} else {
|
|
32
|
+
expanded[originalKey] = value;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return expanded;
|
|
36
|
+
}
|
|
37
|
+
function expand(payload) {
|
|
38
|
+
const shortToKey = new Map(
|
|
39
|
+
Object.entries(payload.k).map(([short, original]) => [short, original])
|
|
40
|
+
);
|
|
41
|
+
if (Array.isArray(payload.d)) {
|
|
42
|
+
return payload.d.map((item) => {
|
|
43
|
+
if (typeof item === "object" && item !== null && !Array.isArray(item)) {
|
|
44
|
+
return expandObject(item, shortToKey, 10);
|
|
45
|
+
}
|
|
46
|
+
return item;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
if (typeof payload.d === "object" && payload.d !== null) {
|
|
50
|
+
return expandObject(payload.d, shortToKey, 10);
|
|
51
|
+
}
|
|
52
|
+
return payload.d;
|
|
53
|
+
}
|
|
54
|
+
function createTerseProxy(compressed, keyMap) {
|
|
55
|
+
const originalToShort = new Map(
|
|
56
|
+
Object.entries(keyMap).map(([short, original]) => [original, short])
|
|
57
|
+
);
|
|
58
|
+
const handler = {
|
|
59
|
+
get(target, prop) {
|
|
60
|
+
if (typeof prop === "symbol") {
|
|
61
|
+
return Reflect.get(target, prop);
|
|
62
|
+
}
|
|
63
|
+
const shortKey = originalToShort.get(prop);
|
|
64
|
+
const actualKey = shortKey ?? prop;
|
|
65
|
+
const value = target[actualKey];
|
|
66
|
+
if (Array.isArray(value)) {
|
|
67
|
+
return value.map((item) => {
|
|
68
|
+
if (typeof item === "object" && item !== null && !Array.isArray(item)) {
|
|
69
|
+
return createTerseProxy(item, keyMap);
|
|
70
|
+
}
|
|
71
|
+
return item;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
if (typeof value === "object" && value !== null) {
|
|
75
|
+
return createTerseProxy(value, keyMap);
|
|
76
|
+
}
|
|
77
|
+
return value;
|
|
78
|
+
},
|
|
79
|
+
has(target, prop) {
|
|
80
|
+
if (typeof prop === "symbol") {
|
|
81
|
+
return Reflect.has(target, prop);
|
|
82
|
+
}
|
|
83
|
+
const shortKey = originalToShort.get(prop);
|
|
84
|
+
return (shortKey ?? prop) in target;
|
|
85
|
+
},
|
|
86
|
+
ownKeys(target) {
|
|
87
|
+
return Object.keys(target).map((shortKey) => keyMap[shortKey] ?? shortKey);
|
|
88
|
+
},
|
|
89
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
90
|
+
if (typeof prop === "symbol") {
|
|
91
|
+
return Reflect.getOwnPropertyDescriptor(target, prop);
|
|
92
|
+
}
|
|
93
|
+
const shortKey = originalToShort.get(prop);
|
|
94
|
+
const actualKey = shortKey ?? prop;
|
|
95
|
+
const descriptor = Object.getOwnPropertyDescriptor(target, actualKey);
|
|
96
|
+
if (descriptor) {
|
|
97
|
+
return { ...descriptor, enumerable: true, configurable: true };
|
|
98
|
+
}
|
|
99
|
+
return void 0;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
return new Proxy(compressed, handler);
|
|
103
|
+
}
|
|
104
|
+
function wrapWithProxy(payload) {
|
|
105
|
+
if (Array.isArray(payload.d)) {
|
|
106
|
+
return payload.d.map((item) => {
|
|
107
|
+
if (typeof item === "object" && item !== null && !Array.isArray(item)) {
|
|
108
|
+
return createTerseProxy(item, payload.k);
|
|
109
|
+
}
|
|
110
|
+
return item;
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
if (typeof payload.d === "object" && payload.d !== null) {
|
|
114
|
+
return createTerseProxy(payload.d, payload.k);
|
|
115
|
+
}
|
|
116
|
+
return payload.d;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// src/client.ts
|
|
120
|
+
var DEFAULT_OPTIONS = {
|
|
121
|
+
headerName: "x-terse-json",
|
|
122
|
+
debug: false,
|
|
123
|
+
autoExpand: true
|
|
124
|
+
};
|
|
125
|
+
function process(data, options = {}) {
|
|
126
|
+
const { useProxy = true } = options;
|
|
127
|
+
if (isTersePayload(data)) {
|
|
128
|
+
return useProxy ? wrapWithProxy(data) : expand(data);
|
|
129
|
+
}
|
|
130
|
+
return data;
|
|
131
|
+
}
|
|
132
|
+
function createFetch(options = {}) {
|
|
133
|
+
const config = { ...DEFAULT_OPTIONS, ...options };
|
|
134
|
+
return async function terseFetch(input, init = {}) {
|
|
135
|
+
const headers = new Headers(init.headers);
|
|
136
|
+
headers.set("accept-terse", "true");
|
|
137
|
+
const response = await globalThis.fetch(input, {
|
|
138
|
+
...init,
|
|
139
|
+
headers
|
|
140
|
+
});
|
|
141
|
+
const isTerse = response.headers.get(config.headerName) === "true";
|
|
142
|
+
if (!isTerse || !config.autoExpand) {
|
|
143
|
+
return response;
|
|
144
|
+
}
|
|
145
|
+
const clonedResponse = response.clone();
|
|
146
|
+
return new Proxy(response, {
|
|
147
|
+
get(target, prop) {
|
|
148
|
+
if (prop === "json") {
|
|
149
|
+
return async function() {
|
|
150
|
+
const data = await clonedResponse.json();
|
|
151
|
+
if (isTersePayload(data)) {
|
|
152
|
+
if (config.debug) {
|
|
153
|
+
console.log("[tersejson] Expanding terse response");
|
|
154
|
+
}
|
|
155
|
+
return wrapWithProxy(data);
|
|
156
|
+
}
|
|
157
|
+
return data;
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
const value = Reflect.get(target, prop);
|
|
161
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
var fetch = createFetch();
|
|
167
|
+
function useTerseFetch(options = {}) {
|
|
168
|
+
return createFetch(options);
|
|
169
|
+
}
|
|
170
|
+
var axiosInterceptor = {
|
|
171
|
+
request: (config) => {
|
|
172
|
+
config.headers = config.headers || {};
|
|
173
|
+
config.headers["accept-terse"] = "true";
|
|
174
|
+
return config;
|
|
175
|
+
},
|
|
176
|
+
response: (response) => {
|
|
177
|
+
const isTerse = response.headers?.["x-terse-json"] === "true";
|
|
178
|
+
if (isTerse && isTersePayload(response.data)) {
|
|
179
|
+
response.data = wrapWithProxy(response.data);
|
|
180
|
+
}
|
|
181
|
+
return response;
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
var client_default = { fetch, createFetch, expand, proxy: wrapWithProxy, process };
|
|
185
|
+
|
|
186
|
+
export { axiosInterceptor, createFetch, client_default as default, expand, fetch, isTersePayload, process, wrapWithProxy as proxy, useTerseFetch };
|
|
187
|
+
//# sourceMappingURL=client.mjs.map
|
|
188
|
+
//# sourceMappingURL=client.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/types.ts","../src/core.ts","../src/client.ts"],"names":[],"mappings":";AAiKO,SAAS,eAAe,KAAA,EAAuC;AACpE,EAAA,OACE,OAAO,KAAA,KAAU,QAAA,IACjB,KAAA,KAAU,QACV,WAAA,IAAe,KAAA,IACd,KAAA,CAAuB,SAAA,KAAc,IAAA,IACtC,GAAA,IAAO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O,aAAa,MAAA,EAAQ,KAAA,EAAO,eAAe,OAAA","file":"client.mjs","sourcesContent":["/**\n * TerseJSON Types\n *\n * Defines the wire format and configuration options for transparent\n * JSON key compression.\n */\n\n/**\n * The compressed format sent over the wire\n */\nexport interface TersePayload<T = unknown> {\n /** Marker to identify this as a TerseJSON payload */\n __terse__: true;\n /** Version for future compatibility */\n v: 1;\n /** Key mapping: short key -> original key */\n k: Record<string, string>;\n /** The compressed data with short keys */\n d: T;\n /** Pattern used for key generation (for debugging/info) */\n p?: string;\n}\n\n/**\n * Built-in key pattern presets\n */\nexport type KeyPatternPreset =\n | 'alpha' // a, b, c, ... z, aa, ab (default)\n | 'numeric' // 0, 1, 2, ... 9, 10, 11\n | 'alphanumeric' // a1, a2, ... a9, b1, b2\n | 'short' // _, __, ___, a, b (shortest possible)\n | 'prefixed'; // k0, k1, k2 (with 'k' prefix)\n\n/**\n * Custom key generator function\n */\nexport type KeyGenerator = (index: number) => string;\n\n/**\n * Key pattern configuration\n */\nexport type KeyPattern =\n | KeyPatternPreset\n | { prefix: string; style?: 'numeric' | 'alpha' }\n | KeyGenerator;\n\n/**\n * How to handle nested structures\n */\nexport type NestedHandling =\n | 'deep' // Compress all nested objects/arrays (default)\n | 'shallow' // Only compress top-level array\n | 'arrays' // Only compress nested arrays, not single objects\n | number; // Specific depth limit (1 = shallow, Infinity = deep)\n\n/**\n * Compression options\n */\nexport interface CompressOptions {\n /**\n * Minimum key length to consider for compression\n * Keys shorter than this won't be shortened\n * @default 3\n */\n minKeyLength?: number;\n\n /**\n * Maximum depth to traverse for nested objects\n * @default 10\n */\n maxDepth?: number;\n\n /**\n * Key pattern to use for generating short keys\n * @default 'alpha'\n */\n keyPattern?: KeyPattern;\n\n /**\n * How to handle nested objects and arrays\n * @default 'deep'\n */\n nestedHandling?: NestedHandling;\n\n /**\n * Only compress keys that appear in all objects (homogeneous)\n * @default false\n */\n homogeneousOnly?: boolean;\n\n /**\n * Keys to always exclude from compression\n */\n excludeKeys?: string[];\n\n /**\n * Keys to always include in compression (even if short)\n */\n includeKeys?: string[];\n}\n\n/**\n * Configuration options for the Express middleware\n */\nexport interface TerseMiddlewareOptions extends CompressOptions {\n /**\n * Minimum array length to trigger compression\n * @default 2\n */\n minArrayLength?: number;\n\n /**\n * Custom function to determine if a response should be compressed\n * Return false to skip compression for specific responses\n */\n shouldCompress?: (data: unknown, req: unknown) => boolean;\n\n /**\n * Custom header name for signaling terse responses\n * @default 'x-terse-json'\n */\n headerName?: string;\n\n /**\n * Enable debug logging\n * @default false\n */\n debug?: boolean;\n}\n\n/**\n * Configuration options for the client\n */\nexport interface TerseClientOptions {\n /**\n * Custom header name to check for terse responses\n * @default 'x-terse-json'\n */\n headerName?: string;\n\n /**\n * Enable debug logging\n * @default false\n */\n debug?: boolean;\n\n /**\n * Automatically expand terse responses\n * @default true\n */\n autoExpand?: boolean;\n}\n\n/**\n * Type helper to preserve original types through compression\n */\nexport type Tersed<T> = T;\n\n/**\n * Check if a value is a TersePayload\n */\nexport function isTersePayload(value: unknown): value is TersePayload {\n return (\n typeof value === 'object' &&\n value !== null &&\n '__terse__' in value &&\n (value as TersePayload).__terse__ === true &&\n 'v' in value &&\n 'k' in value &&\n 'd' in value\n );\n}\n","/**\n * TerseJSON Core\n *\n * The core compression and expansion algorithms.\n */\n\nimport {\n TersePayload,\n isTersePayload,\n KeyPattern,\n KeyGenerator,\n NestedHandling,\n CompressOptions,\n} from './types';\n\n// ============================================================================\n// KEY PATTERN GENERATORS\n// ============================================================================\n\n/**\n * Alpha pattern: a, b, c, ... z, aa, ab, ...\n */\nfunction alphaGenerator(index: number): string {\n let key = '';\n let remaining = index;\n\n do {\n key = String.fromCharCode(97 + (remaining % 26)) + key;\n remaining = Math.floor(remaining / 26) - 1;\n } while (remaining >= 0);\n\n return key;\n}\n\n/**\n * Numeric pattern: 0, 1, 2, ... 9, 10, 11, ...\n */\nfunction numericGenerator(index: number): string {\n return String(index);\n}\n\n/**\n * Alphanumeric pattern: a1, a2, ... a9, b1, b2, ...\n */\nfunction alphanumericGenerator(index: number): string {\n const letterIndex = Math.floor(index / 9);\n const numIndex = (index % 9) + 1;\n return alphaGenerator(letterIndex) + numIndex;\n}\n\n/**\n * Short pattern: uses shortest possible keys\n * _, a, b, ..., z, aa, ab, ...\n */\nfunction shortGenerator(index: number): string {\n if (index === 0) return '_';\n return alphaGenerator(index - 1);\n}\n\n/**\n * Prefixed pattern: k0, k1, k2, ...\n */\nfunction prefixedGenerator(prefix: string, style: 'numeric' | 'alpha' = 'numeric'): KeyGenerator {\n return (index: number) => {\n if (style === 'alpha') {\n return prefix + alphaGenerator(index);\n }\n return prefix + index;\n };\n}\n\n/**\n * Creates a key generator from a pattern configuration\n */\nexport function createKeyGenerator(pattern: KeyPattern): { generator: KeyGenerator; name: string } {\n // If it's already a function, use it directly\n if (typeof pattern === 'function') {\n return { generator: pattern, name: 'custom' };\n }\n\n // If it's a preset string\n if (typeof pattern === 'string') {\n switch (pattern) {\n case 'alpha':\n return { generator: alphaGenerator, name: 'alpha' };\n case 'numeric':\n return { generator: numericGenerator, name: 'numeric' };\n case 'alphanumeric':\n return { generator: alphanumericGenerator, name: 'alphanumeric' };\n case 'short':\n return { generator: shortGenerator, name: 'short' };\n case 'prefixed':\n return { generator: prefixedGenerator('k'), name: 'prefixed:k' };\n default:\n return { generator: alphaGenerator, name: 'alpha' };\n }\n }\n\n // If it's a prefix config object\n if (typeof pattern === 'object' && 'prefix' in pattern) {\n return {\n generator: prefixedGenerator(pattern.prefix, pattern.style || 'numeric'),\n name: `prefixed:${pattern.prefix}`,\n };\n }\n\n return { generator: alphaGenerator, name: 'alpha' };\n}\n\n/**\n * Resolves nested handling to a numeric depth\n */\nfunction resolveNestedDepth(handling: NestedHandling | undefined, maxDepth: number): number {\n if (handling === undefined || handling === 'deep') {\n return maxDepth;\n }\n if (handling === 'shallow') {\n return 1;\n }\n if (handling === 'arrays') {\n return maxDepth; // Special handling in collect/compress functions\n }\n if (typeof handling === 'number') {\n return handling;\n }\n return maxDepth;\n}\n\n// Legacy generateShortKey removed - use createKeyGenerator instead\n\ninterface CollectKeysOptions {\n minKeyLength: number;\n maxDepth: number;\n nestedHandling: NestedHandling;\n excludeKeys?: string[];\n includeKeys?: string[];\n homogeneousOnly?: boolean;\n}\n\n/**\n * Collects all unique keys from an array of objects\n */\nfunction collectKeys(\n data: Record<string, unknown>[],\n options: CollectKeysOptions,\n currentDepth: number = 0\n): Set<string> {\n const keys = new Set<string>();\n const { minKeyLength, maxDepth, nestedHandling, excludeKeys, includeKeys } = options;\n\n if (currentDepth >= maxDepth) return keys;\n\n // For homogeneous mode, track key counts\n const keyCounts = new Map<string, number>();\n\n for (const item of data) {\n if (typeof item !== 'object' || item === null) continue;\n\n for (const key of Object.keys(item)) {\n // Skip excluded keys\n if (excludeKeys?.includes(key)) continue;\n\n // Include if in includeKeys, or if meets minKeyLength\n const shouldInclude = includeKeys?.includes(key) || key.length >= minKeyLength;\n\n if (shouldInclude) {\n keys.add(key);\n keyCounts.set(key, (keyCounts.get(key) || 0) + 1);\n }\n\n // Handle nested structures based on nestedHandling option\n const value = item[key];\n\n if (nestedHandling === 'shallow') {\n // Don't process nested structures\n continue;\n }\n\n if (Array.isArray(value) && value.length > 0 && isCompressibleArray(value)) {\n // Nested array of objects - always process\n const nestedKeys = collectKeys(\n value as Record<string, unknown>[],\n options,\n currentDepth + 1\n );\n nestedKeys.forEach(k => keys.add(k));\n } else if (\n nestedHandling !== 'arrays' &&\n typeof value === 'object' &&\n value !== null &&\n !Array.isArray(value)\n ) {\n // Single nested object - skip if nestedHandling is 'arrays'\n const nestedKeys = collectKeys(\n [value as Record<string, unknown>],\n options,\n currentDepth + 1\n );\n nestedKeys.forEach(k => keys.add(k));\n }\n }\n }\n\n // If homogeneous mode, only keep keys that appear in ALL objects\n if (options.homogeneousOnly && data.length > 0) {\n for (const [key, count] of keyCounts) {\n if (count < data.length) {\n keys.delete(key);\n }\n }\n }\n\n return keys;\n}\n\n/**\n * Checks if an array is compressible (array of objects with consistent structure)\n */\nexport function isCompressibleArray(data: unknown): data is Record<string, unknown>[] {\n if (!Array.isArray(data) || data.length === 0) return false;\n\n // Check if all items are objects\n return data.every(\n item => typeof item === 'object' && item !== null && !Array.isArray(item)\n );\n}\n\n/**\n * Compresses an object using the key mapping\n */\nfunction compressObject(\n obj: Record<string, unknown>,\n keyToShort: Map<string, string>,\n maxDepth: number,\n currentDepth: number = 0\n): Record<string, unknown> {\n if (currentDepth >= maxDepth) return obj;\n\n const compressed: Record<string, unknown> = {};\n\n for (const [key, value] of Object.entries(obj)) {\n const shortKey = keyToShort.get(key) ?? key;\n\n if (Array.isArray(value) && isCompressibleArray(value)) {\n // Recursively compress nested arrays\n compressed[shortKey] = value.map(item =>\n compressObject(\n item as Record<string, unknown>,\n keyToShort,\n maxDepth,\n currentDepth + 1\n )\n );\n } else if (typeof value === 'object' && value !== null && !Array.isArray(value)) {\n // Compress single nested objects too\n compressed[shortKey] = compressObject(\n value as Record<string, unknown>,\n keyToShort,\n maxDepth,\n currentDepth + 1\n );\n } else {\n compressed[shortKey] = value;\n }\n }\n\n return compressed;\n}\n\n/**\n * Expands an object using the key mapping\n */\nfunction expandObject(\n obj: Record<string, unknown>,\n shortToKey: Map<string, string>,\n maxDepth: number,\n currentDepth: number = 0\n): Record<string, unknown> {\n if (currentDepth >= maxDepth) return obj;\n\n const expanded: Record<string, unknown> = {};\n\n for (const [shortKey, value] of Object.entries(obj)) {\n const originalKey = shortToKey.get(shortKey) ?? shortKey;\n\n if (Array.isArray(value)) {\n expanded[originalKey] = value.map(item => {\n if (typeof item === 'object' && item !== null && !Array.isArray(item)) {\n return expandObject(\n item as Record<string, unknown>,\n shortToKey,\n maxDepth,\n currentDepth + 1\n );\n }\n return item;\n });\n } else if (typeof value === 'object' && value !== null) {\n expanded[originalKey] = expandObject(\n value as Record<string, unknown>,\n shortToKey,\n maxDepth,\n currentDepth + 1\n );\n } else {\n expanded[originalKey] = value;\n }\n }\n\n return expanded;\n}\n\n// Re-export CompressOptions from types for backwards compatibility\nexport type { CompressOptions } from './types';\n\n/**\n * Compresses an array of objects by replacing keys with short aliases\n */\nexport function compress<T extends Record<string, unknown>[]>(\n data: T,\n options: CompressOptions = {}\n): TersePayload<unknown[]> {\n const {\n minKeyLength = 3,\n maxDepth = 10,\n keyPattern = 'alpha',\n nestedHandling = 'deep',\n homogeneousOnly = false,\n excludeKeys,\n includeKeys,\n } = options;\n\n // Create key generator\n const { generator, name: patternName } = createKeyGenerator(keyPattern);\n\n // Resolve nested depth\n const effectiveDepth = resolveNestedDepth(nestedHandling, maxDepth);\n\n // Collect all unique keys\n const allKeys = collectKeys(data, {\n minKeyLength,\n maxDepth: effectiveDepth,\n nestedHandling,\n excludeKeys,\n includeKeys,\n homogeneousOnly,\n });\n\n // Sort keys by frequency of use (most used first) for optimal compression\n // For now, just sort alphabetically for deterministic output\n const sortedKeys = Array.from(allKeys).sort();\n\n // Create bidirectional mapping\n const keyToShort = new Map<string, string>();\n const keyMap: Record<string, string> = {};\n\n sortedKeys.forEach((key, index) => {\n const shortKey = generator(index);\n // Only use short key if it's actually shorter\n if (shortKey.length < key.length) {\n keyToShort.set(key, shortKey);\n keyMap[shortKey] = key;\n }\n });\n\n // Compress the data\n const compressed = data.map(item =>\n compressObject(item, keyToShort, effectiveDepth)\n );\n\n return {\n __terse__: true,\n v: 1,\n k: keyMap,\n d: compressed,\n p: patternName,\n };\n}\n\n/**\n * Expands a TersePayload back to its original form\n */\nexport function expand<T = unknown>(payload: TersePayload): T {\n const shortToKey = new Map(\n Object.entries(payload.k).map(([short, original]) => [short, original])\n );\n\n if (Array.isArray(payload.d)) {\n return payload.d.map(item => {\n if (typeof item === 'object' && item !== null && !Array.isArray(item)) {\n return expandObject(item as Record<string, unknown>, shortToKey, 10);\n }\n return item;\n }) as T;\n }\n\n if (typeof payload.d === 'object' && payload.d !== null) {\n return expandObject(payload.d as Record<string, unknown>, shortToKey, 10) as T;\n }\n\n return payload.d as T;\n}\n\n/**\n * Creates a Proxy that transparently maps original keys to short keys\n * This is the magic that makes client-side access seamless\n */\nexport function createTerseProxy<T extends Record<string, unknown>>(\n compressed: Record<string, unknown>,\n keyMap: Record<string, string> // short -> original\n): T {\n // Create reverse map: original -> short\n const originalToShort = new Map(\n Object.entries(keyMap).map(([short, original]) => [original, short])\n );\n\n const handler: ProxyHandler<Record<string, unknown>> = {\n get(target, prop: string | symbol) {\n if (typeof prop === 'symbol') {\n return Reflect.get(target, prop);\n }\n\n // Check if accessing by original key name\n const shortKey = originalToShort.get(prop);\n const actualKey = shortKey ?? prop;\n const value = target[actualKey];\n\n // Recursively proxy nested objects/arrays\n if (Array.isArray(value)) {\n return value.map(item => {\n if (typeof item === 'object' && item !== null && !Array.isArray(item)) {\n return createTerseProxy(item as Record<string, unknown>, keyMap);\n }\n return item;\n });\n }\n\n if (typeof value === 'object' && value !== null) {\n return createTerseProxy(value as Record<string, unknown>, keyMap);\n }\n\n return value;\n },\n\n has(target, prop: string | symbol) {\n if (typeof prop === 'symbol') {\n return Reflect.has(target, prop);\n }\n const shortKey = originalToShort.get(prop);\n return (shortKey ?? prop) in target;\n },\n\n ownKeys(target) {\n // Return original key names\n return Object.keys(target).map(shortKey => keyMap[shortKey] ?? shortKey);\n },\n\n getOwnPropertyDescriptor(target, prop: string | symbol) {\n if (typeof prop === 'symbol') {\n return Reflect.getOwnPropertyDescriptor(target, prop);\n }\n const shortKey = originalToShort.get(prop);\n const actualKey = shortKey ?? prop;\n const descriptor = Object.getOwnPropertyDescriptor(target, actualKey);\n if (descriptor) {\n return { ...descriptor, enumerable: true, configurable: true };\n }\n return undefined;\n },\n };\n\n return new Proxy(compressed, handler) as T;\n}\n\n/**\n * Wraps TersePayload data with Proxies for transparent access\n */\nexport function wrapWithProxy<T>(payload: TersePayload): T {\n if (Array.isArray(payload.d)) {\n return payload.d.map(item => {\n if (typeof item === 'object' && item !== null && !Array.isArray(item)) {\n return createTerseProxy(item as Record<string, unknown>, payload.k);\n }\n return item;\n }) as T;\n }\n\n if (typeof payload.d === 'object' && payload.d !== null) {\n return createTerseProxy(payload.d as Record<string, unknown>, payload.k) as T;\n }\n\n return payload.d as T;\n}\n\n// Re-export for convenience\nexport { isTersePayload };\n","/**\n * TerseJSON Client\n *\n * Transparent client-side handling of TerseJSON responses.\n * Use the fetch wrapper or manually expand responses.\n */\n\nimport {\n TerseClientOptions,\n isTersePayload,\n} from './types';\nimport { expand, wrapWithProxy } from './core';\n\nconst DEFAULT_OPTIONS: Required<TerseClientOptions> = {\n headerName: 'x-terse-json',\n debug: false,\n autoExpand: true,\n};\n\n/**\n * Expands a TerseJSON payload back to original format\n * Use this if you want full expansion (not proxied)\n */\nexport { expand };\n\n/**\n * Wraps a TerseJSON payload with Proxy for transparent key access\n * More memory efficient than full expansion for large datasets\n */\nexport { wrapWithProxy as proxy };\n\n/**\n * Check if a response/data is a TerseJSON payload\n */\nexport { isTersePayload };\n\n/**\n * Process a potential TerseJSON response\n * Automatically detects and expands/proxies terse payloads\n */\nexport function process<T = unknown>(\n data: unknown,\n options: { useProxy?: boolean } = {}\n): T {\n const { useProxy = true } = options;\n\n if (isTersePayload(data)) {\n return useProxy ? wrapWithProxy<T>(data) : expand<T>(data);\n }\n\n return data as T;\n}\n\n/**\n * Creates a fetch wrapper that automatically handles TerseJSON responses\n *\n * @example\n * ```typescript\n * import { createFetch } from 'tersejson/client';\n *\n * const fetch = createFetch();\n *\n * // Use exactly like regular fetch\n * const users = await fetch('/api/users').then(r => r.json());\n * console.log(users[0].firstName); // Works transparently!\n * ```\n */\nexport function createFetch(options: TerseClientOptions = {}): typeof globalThis.fetch {\n const config = { ...DEFAULT_OPTIONS, ...options };\n\n return async function terseFetch(\n input: RequestInfo | URL,\n init: RequestInit = {}\n ): Promise<Response> {\n // Add header to indicate we accept terse responses\n const headers = new Headers(init.headers);\n headers.set('accept-terse', 'true');\n\n const response = await globalThis.fetch(input, {\n ...init,\n headers,\n });\n\n // Check if response is terse\n const isTerse = response.headers.get(config.headerName) === 'true';\n\n if (!isTerse || !config.autoExpand) {\n return response;\n }\n\n // Clone response and override json() method\n const clonedResponse = response.clone();\n\n // Create a wrapper that intercepts .json()\n return new Proxy(response, {\n get(target, prop) {\n if (prop === 'json') {\n return async function (): Promise<unknown> {\n const data = await clonedResponse.json();\n\n if (isTersePayload(data)) {\n if (config.debug) {\n console.log('[tersejson] Expanding terse response');\n }\n return wrapWithProxy(data);\n }\n\n return data;\n };\n }\n\n const value = Reflect.get(target, prop);\n return typeof value === 'function' ? value.bind(target) : value;\n },\n });\n };\n}\n\n/**\n * Drop-in fetch replacement with TerseJSON support\n *\n * @example\n * ```typescript\n * import { fetch } from 'tersejson/client';\n *\n * const users = await fetch('/api/users').then(r => r.json());\n * console.log(users[0].firstName); // Transparent!\n * ```\n */\nexport const fetch = createFetch();\n\n/**\n * React hook for fetching with TerseJSON support (if using React)\n * Returns a configured fetch function\n *\n * @example\n * ```typescript\n * function MyComponent() {\n * const terseFetch = useTerseFetch();\n * // Use in useEffect, etc.\n * }\n * ```\n */\nexport function useTerseFetch(options: TerseClientOptions = {}): typeof globalThis.fetch {\n // This is a simple hook that just returns the configured fetch\n // Could be extended with caching, SWR integration, etc.\n return createFetch(options);\n}\n\n/**\n * Axios interceptor for TerseJSON support\n *\n * @example\n * ```typescript\n * import axios from 'axios';\n * import { axiosInterceptor } from 'tersejson/client';\n *\n * axios.interceptors.request.use(axiosInterceptor.request);\n * axios.interceptors.response.use(axiosInterceptor.response);\n * ```\n */\nexport const axiosInterceptor = {\n request: (config: { headers?: Record<string, string> }) => {\n config.headers = config.headers || {};\n config.headers['accept-terse'] = 'true';\n return config;\n },\n\n response: (response: { headers?: Record<string, string>; data?: unknown }) => {\n const isTerse = response.headers?.['x-terse-json'] === 'true';\n\n if (isTerse && isTersePayload(response.data)) {\n response.data = wrapWithProxy(response.data);\n }\n\n return response;\n },\n};\n\n// Default export for convenience\nexport default { fetch, createFetch, expand, proxy: wrapWithProxy, process };\n"]}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { RequestHandler } from 'express';
|
|
2
|
+
import { a as TerseMiddlewareOptions } from './types-CzaGQaV7.mjs';
|
|
3
|
+
import analytics, { AnalyticsConfig, TerseAnalytics, AnalyticsStats, CompressionEvent, getAnalytics, initAnalytics } from './analytics.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* TerseJSON Express Middleware
|
|
7
|
+
*
|
|
8
|
+
* Automatically compresses JSON responses with repeated object structures.
|
|
9
|
+
* Zero configuration required - just add the middleware.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Extended middleware options with analytics
|
|
14
|
+
*/
|
|
15
|
+
interface TerseMiddlewareOptionsWithAnalytics extends TerseMiddlewareOptions {
|
|
16
|
+
/**
|
|
17
|
+
* Analytics configuration
|
|
18
|
+
* Set to true for local-only analytics
|
|
19
|
+
* Set to { apiKey: 'xxx' } for cloud reporting
|
|
20
|
+
*/
|
|
21
|
+
analytics?: boolean | Partial<AnalyticsConfig> | TerseAnalytics;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Creates the TerseJSON Express middleware
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* import express from 'express';
|
|
29
|
+
* import { terse } from 'tersejson/express';
|
|
30
|
+
*
|
|
31
|
+
* const app = express();
|
|
32
|
+
* app.use(terse());
|
|
33
|
+
*
|
|
34
|
+
* app.get('/users', (req, res) => {
|
|
35
|
+
* // Just send data as normal - compression is automatic
|
|
36
|
+
* res.json(users);
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
declare function terse(options?: TerseMiddlewareOptionsWithAnalytics): RequestHandler;
|
|
41
|
+
/**
|
|
42
|
+
* Middleware to automatically add x-accept-terse header based on query param
|
|
43
|
+
* Useful for testing or when you can't control client headers
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* app.use(terseQueryParam());
|
|
48
|
+
* // Now ?terse=true will enable compression
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare function terseQueryParam(paramName?: string): RequestHandler;
|
|
52
|
+
|
|
53
|
+
declare const express_AnalyticsConfig: typeof AnalyticsConfig;
|
|
54
|
+
declare const express_AnalyticsStats: typeof AnalyticsStats;
|
|
55
|
+
declare const express_CompressionEvent: typeof CompressionEvent;
|
|
56
|
+
declare const express_TerseAnalytics: typeof TerseAnalytics;
|
|
57
|
+
type express_TerseMiddlewareOptionsWithAnalytics = TerseMiddlewareOptionsWithAnalytics;
|
|
58
|
+
declare const express_analytics: typeof analytics;
|
|
59
|
+
declare const express_getAnalytics: typeof getAnalytics;
|
|
60
|
+
declare const express_initAnalytics: typeof initAnalytics;
|
|
61
|
+
declare const express_terse: typeof terse;
|
|
62
|
+
declare const express_terseQueryParam: typeof terseQueryParam;
|
|
63
|
+
declare namespace express {
|
|
64
|
+
export { express_AnalyticsConfig as AnalyticsConfig, express_AnalyticsStats as AnalyticsStats, express_CompressionEvent as CompressionEvent, express_TerseAnalytics as TerseAnalytics, type express_TerseMiddlewareOptionsWithAnalytics as TerseMiddlewareOptionsWithAnalytics, express_analytics as analytics, terse as default, express_getAnalytics as getAnalytics, express_initAnalytics as initAnalytics, express_terse as terse, express_terseQueryParam as terseQueryParam };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { type TerseMiddlewareOptionsWithAnalytics as T, terseQueryParam as a, express as e, terse as t };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { RequestHandler } from 'express';
|
|
2
|
+
import { a as TerseMiddlewareOptions } from './types-CzaGQaV7.js';
|
|
3
|
+
import analytics, { AnalyticsConfig, TerseAnalytics, AnalyticsStats, CompressionEvent, getAnalytics, initAnalytics } from './analytics.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* TerseJSON Express Middleware
|
|
7
|
+
*
|
|
8
|
+
* Automatically compresses JSON responses with repeated object structures.
|
|
9
|
+
* Zero configuration required - just add the middleware.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Extended middleware options with analytics
|
|
14
|
+
*/
|
|
15
|
+
interface TerseMiddlewareOptionsWithAnalytics extends TerseMiddlewareOptions {
|
|
16
|
+
/**
|
|
17
|
+
* Analytics configuration
|
|
18
|
+
* Set to true for local-only analytics
|
|
19
|
+
* Set to { apiKey: 'xxx' } for cloud reporting
|
|
20
|
+
*/
|
|
21
|
+
analytics?: boolean | Partial<AnalyticsConfig> | TerseAnalytics;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Creates the TerseJSON Express middleware
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* import express from 'express';
|
|
29
|
+
* import { terse } from 'tersejson/express';
|
|
30
|
+
*
|
|
31
|
+
* const app = express();
|
|
32
|
+
* app.use(terse());
|
|
33
|
+
*
|
|
34
|
+
* app.get('/users', (req, res) => {
|
|
35
|
+
* // Just send data as normal - compression is automatic
|
|
36
|
+
* res.json(users);
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
declare function terse(options?: TerseMiddlewareOptionsWithAnalytics): RequestHandler;
|
|
41
|
+
/**
|
|
42
|
+
* Middleware to automatically add x-accept-terse header based on query param
|
|
43
|
+
* Useful for testing or when you can't control client headers
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* app.use(terseQueryParam());
|
|
48
|
+
* // Now ?terse=true will enable compression
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare function terseQueryParam(paramName?: string): RequestHandler;
|
|
52
|
+
|
|
53
|
+
declare const express_AnalyticsConfig: typeof AnalyticsConfig;
|
|
54
|
+
declare const express_AnalyticsStats: typeof AnalyticsStats;
|
|
55
|
+
declare const express_CompressionEvent: typeof CompressionEvent;
|
|
56
|
+
declare const express_TerseAnalytics: typeof TerseAnalytics;
|
|
57
|
+
type express_TerseMiddlewareOptionsWithAnalytics = TerseMiddlewareOptionsWithAnalytics;
|
|
58
|
+
declare const express_analytics: typeof analytics;
|
|
59
|
+
declare const express_getAnalytics: typeof getAnalytics;
|
|
60
|
+
declare const express_initAnalytics: typeof initAnalytics;
|
|
61
|
+
declare const express_terse: typeof terse;
|
|
62
|
+
declare const express_terseQueryParam: typeof terseQueryParam;
|
|
63
|
+
declare namespace express {
|
|
64
|
+
export { express_AnalyticsConfig as AnalyticsConfig, express_AnalyticsStats as AnalyticsStats, express_CompressionEvent as CompressionEvent, express_TerseAnalytics as TerseAnalytics, type express_TerseMiddlewareOptionsWithAnalytics as TerseMiddlewareOptionsWithAnalytics, express_analytics as analytics, terse as default, express_getAnalytics as getAnalytics, express_initAnalytics as initAnalytics, express_terse as terse, express_terseQueryParam as terseQueryParam };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { type TerseMiddlewareOptionsWithAnalytics as T, terseQueryParam as a, express as e, terse as t };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import 'express';
|
|
2
|
+
import './types-CzaGQaV7.mjs';
|
|
3
|
+
export { AnalyticsConfig, AnalyticsStats, CompressionEvent, TerseAnalytics, default as analytics, getAnalytics, initAnalytics } from './analytics.mjs';
|
|
4
|
+
export { T as TerseMiddlewareOptionsWithAnalytics, t as default, t as terse, a as terseQueryParam } from './express-BoL__Ao6.mjs';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import 'express';
|
|
2
|
+
import './types-CzaGQaV7.js';
|
|
3
|
+
export { AnalyticsConfig, AnalyticsStats, CompressionEvent, TerseAnalytics, default as analytics, getAnalytics, initAnalytics } from './analytics.js';
|
|
4
|
+
export { T as TerseMiddlewareOptionsWithAnalytics, t as default, t as terse, a as terseQueryParam } from './express-LSVylWpN.js';
|