tersejson 0.2.0 → 0.3.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,167 @@
1
+ import { C as CompressOptions, T as TersePayload } from './types-BTonKlz8.mjs';
2
+
3
+ /**
4
+ * TerseJSON Server-Side Memory Optimization
5
+ *
6
+ * Utilities for memory-efficient data handling on the server:
7
+ * - TerseCache: Store compressed data, return Proxy-wrapped on access
8
+ * - compressStream: Compress data as it streams from database cursors
9
+ * - createTerseServiceClient: Inter-service communication with compressed payloads
10
+ */
11
+
12
+ /**
13
+ * Options for TerseCache
14
+ */
15
+ interface TerseCacheOptions {
16
+ /** Compression options passed to compress() */
17
+ compressOptions?: CompressOptions;
18
+ /** Maximum number of entries (simple LRU when exceeded) */
19
+ maxSize?: number;
20
+ /** Default TTL in milliseconds (optional) */
21
+ defaultTTL?: number;
22
+ }
23
+ /**
24
+ * A memory-efficient cache that stores data in compressed form.
25
+ *
26
+ * Data is compressed on set() and stays compressed in memory.
27
+ * On get(), data is returned wrapped in a Proxy for lazy expansion.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * const cache = new TerseCache<User[]>();
32
+ *
33
+ * // Store compressed - uses less memory
34
+ * cache.set('users', largeUserArray);
35
+ *
36
+ * // Get returns Proxy-wrapped data
37
+ * const users = cache.get('users');
38
+ * users?.map(u => u.name); // Only 'name' field expands
39
+ * ```
40
+ */
41
+ declare class TerseCache<T extends Record<string, unknown>[] = Record<string, unknown>[]> {
42
+ private cache;
43
+ private options;
44
+ private accessOrder;
45
+ constructor(options?: TerseCacheOptions);
46
+ /**
47
+ * Store data in compressed form
48
+ */
49
+ set(key: string, data: T, ttl?: number): void;
50
+ /**
51
+ * Get data wrapped in Proxy for lazy expansion.
52
+ * Only accessed fields are expanded from compressed form.
53
+ */
54
+ get(key: string): T | undefined;
55
+ /**
56
+ * Get the raw compressed payload without expansion.
57
+ * Useful for forwarding to other services.
58
+ */
59
+ getRaw(key: string): TersePayload<T> | undefined;
60
+ /**
61
+ * Check if key exists (and is not expired)
62
+ */
63
+ has(key: string): boolean;
64
+ /**
65
+ * Delete an entry
66
+ */
67
+ delete(key: string): boolean;
68
+ /**
69
+ * Clear all entries
70
+ */
71
+ clear(): void;
72
+ /**
73
+ * Get current cache size
74
+ */
75
+ get size(): number;
76
+ private updateAccessOrder;
77
+ private removeFromAccessOrder;
78
+ }
79
+ /**
80
+ * Options for streaming compression
81
+ */
82
+ interface CompressStreamOptions extends CompressOptions {
83
+ /** Number of items per batch (default: 100) */
84
+ batchSize?: number;
85
+ }
86
+ /**
87
+ * Compress data as it streams from an async source (e.g., database cursor).
88
+ *
89
+ * Yields compressed batches without loading entire result set into memory.
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * // MongoDB cursor
94
+ * const cursor = db.collection('users').find().stream();
95
+ * for await (const batch of compressStream(cursor, { batchSize: 100 })) {
96
+ * // Send batch to client or process
97
+ * res.write(JSON.stringify(batch));
98
+ * }
99
+ *
100
+ * // PostgreSQL stream
101
+ * const stream = client.query(new QueryStream('SELECT * FROM users'));
102
+ * for await (const batch of compressStream(stream)) {
103
+ * // Process compressed batches
104
+ * }
105
+ * ```
106
+ */
107
+ declare function compressStream<T extends Record<string, unknown>>(source: AsyncIterable<T>, options?: CompressStreamOptions): AsyncGenerator<TersePayload<T[]>, void, unknown>;
108
+ /**
109
+ * Configuration for TerseServiceClient
110
+ */
111
+ interface TerseServiceClientConfig {
112
+ /** Base URL for the service */
113
+ baseUrl: string;
114
+ /** Whether to expand responses with Proxy (default: true) */
115
+ expandOnReceive?: boolean;
116
+ /** Custom headers to include in all requests */
117
+ headers?: Record<string, string>;
118
+ /** Custom fetch implementation (for testing or Node.js < 18) */
119
+ fetch?: typeof globalThis.fetch;
120
+ }
121
+ /**
122
+ * HTTP client for inter-service communication with TerseJSON.
123
+ *
124
+ * Services can pass compressed data without intermediate expansion,
125
+ * reducing memory usage across the request chain.
126
+ *
127
+ * @example
128
+ * ```typescript
129
+ * const serviceB = createTerseServiceClient({
130
+ * baseUrl: 'http://service-b:3000',
131
+ * });
132
+ *
133
+ * // GET - returns Proxy-wrapped data
134
+ * const users = await serviceB.get<User[]>('/api/users');
135
+ * users.map(u => u.name); // Only accessed fields expand
136
+ *
137
+ * // Forward compressed payload to another service
138
+ * const raw = cache.getRaw('users');
139
+ * if (raw) {
140
+ * await serviceB.forward('/api/users/sync', raw);
141
+ * }
142
+ * ```
143
+ */
144
+ declare function createTerseServiceClient(config: TerseServiceClientConfig): {
145
+ /**
146
+ * GET request - receives TerseJSON and returns Proxy-wrapped (or raw)
147
+ */
148
+ get<T>(path: string, options?: {
149
+ headers?: Record<string, string>;
150
+ }): Promise<T>;
151
+ /**
152
+ * POST request with automatic compression
153
+ */
154
+ post<T, R = unknown>(path: string, data: T, options?: {
155
+ headers?: Record<string, string>;
156
+ compress?: boolean;
157
+ }): Promise<R>;
158
+ /**
159
+ * Forward a raw TersePayload to another endpoint without expansion.
160
+ * Useful for passing data between services without intermediate deserialization.
161
+ */
162
+ forward<T extends Record<string, unknown>[]>(path: string, payload: TersePayload<T>, options?: {
163
+ headers?: Record<string, string>;
164
+ }): Promise<void>;
165
+ };
166
+
167
+ export { CompressOptions, type CompressStreamOptions, TerseCache, type TerseCacheOptions, TersePayload, type TerseServiceClientConfig, compressStream, createTerseServiceClient };
@@ -0,0 +1,167 @@
1
+ import { C as CompressOptions, T as TersePayload } from './types-BTonKlz8.js';
2
+
3
+ /**
4
+ * TerseJSON Server-Side Memory Optimization
5
+ *
6
+ * Utilities for memory-efficient data handling on the server:
7
+ * - TerseCache: Store compressed data, return Proxy-wrapped on access
8
+ * - compressStream: Compress data as it streams from database cursors
9
+ * - createTerseServiceClient: Inter-service communication with compressed payloads
10
+ */
11
+
12
+ /**
13
+ * Options for TerseCache
14
+ */
15
+ interface TerseCacheOptions {
16
+ /** Compression options passed to compress() */
17
+ compressOptions?: CompressOptions;
18
+ /** Maximum number of entries (simple LRU when exceeded) */
19
+ maxSize?: number;
20
+ /** Default TTL in milliseconds (optional) */
21
+ defaultTTL?: number;
22
+ }
23
+ /**
24
+ * A memory-efficient cache that stores data in compressed form.
25
+ *
26
+ * Data is compressed on set() and stays compressed in memory.
27
+ * On get(), data is returned wrapped in a Proxy for lazy expansion.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * const cache = new TerseCache<User[]>();
32
+ *
33
+ * // Store compressed - uses less memory
34
+ * cache.set('users', largeUserArray);
35
+ *
36
+ * // Get returns Proxy-wrapped data
37
+ * const users = cache.get('users');
38
+ * users?.map(u => u.name); // Only 'name' field expands
39
+ * ```
40
+ */
41
+ declare class TerseCache<T extends Record<string, unknown>[] = Record<string, unknown>[]> {
42
+ private cache;
43
+ private options;
44
+ private accessOrder;
45
+ constructor(options?: TerseCacheOptions);
46
+ /**
47
+ * Store data in compressed form
48
+ */
49
+ set(key: string, data: T, ttl?: number): void;
50
+ /**
51
+ * Get data wrapped in Proxy for lazy expansion.
52
+ * Only accessed fields are expanded from compressed form.
53
+ */
54
+ get(key: string): T | undefined;
55
+ /**
56
+ * Get the raw compressed payload without expansion.
57
+ * Useful for forwarding to other services.
58
+ */
59
+ getRaw(key: string): TersePayload<T> | undefined;
60
+ /**
61
+ * Check if key exists (and is not expired)
62
+ */
63
+ has(key: string): boolean;
64
+ /**
65
+ * Delete an entry
66
+ */
67
+ delete(key: string): boolean;
68
+ /**
69
+ * Clear all entries
70
+ */
71
+ clear(): void;
72
+ /**
73
+ * Get current cache size
74
+ */
75
+ get size(): number;
76
+ private updateAccessOrder;
77
+ private removeFromAccessOrder;
78
+ }
79
+ /**
80
+ * Options for streaming compression
81
+ */
82
+ interface CompressStreamOptions extends CompressOptions {
83
+ /** Number of items per batch (default: 100) */
84
+ batchSize?: number;
85
+ }
86
+ /**
87
+ * Compress data as it streams from an async source (e.g., database cursor).
88
+ *
89
+ * Yields compressed batches without loading entire result set into memory.
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * // MongoDB cursor
94
+ * const cursor = db.collection('users').find().stream();
95
+ * for await (const batch of compressStream(cursor, { batchSize: 100 })) {
96
+ * // Send batch to client or process
97
+ * res.write(JSON.stringify(batch));
98
+ * }
99
+ *
100
+ * // PostgreSQL stream
101
+ * const stream = client.query(new QueryStream('SELECT * FROM users'));
102
+ * for await (const batch of compressStream(stream)) {
103
+ * // Process compressed batches
104
+ * }
105
+ * ```
106
+ */
107
+ declare function compressStream<T extends Record<string, unknown>>(source: AsyncIterable<T>, options?: CompressStreamOptions): AsyncGenerator<TersePayload<T[]>, void, unknown>;
108
+ /**
109
+ * Configuration for TerseServiceClient
110
+ */
111
+ interface TerseServiceClientConfig {
112
+ /** Base URL for the service */
113
+ baseUrl: string;
114
+ /** Whether to expand responses with Proxy (default: true) */
115
+ expandOnReceive?: boolean;
116
+ /** Custom headers to include in all requests */
117
+ headers?: Record<string, string>;
118
+ /** Custom fetch implementation (for testing or Node.js < 18) */
119
+ fetch?: typeof globalThis.fetch;
120
+ }
121
+ /**
122
+ * HTTP client for inter-service communication with TerseJSON.
123
+ *
124
+ * Services can pass compressed data without intermediate expansion,
125
+ * reducing memory usage across the request chain.
126
+ *
127
+ * @example
128
+ * ```typescript
129
+ * const serviceB = createTerseServiceClient({
130
+ * baseUrl: 'http://service-b:3000',
131
+ * });
132
+ *
133
+ * // GET - returns Proxy-wrapped data
134
+ * const users = await serviceB.get<User[]>('/api/users');
135
+ * users.map(u => u.name); // Only accessed fields expand
136
+ *
137
+ * // Forward compressed payload to another service
138
+ * const raw = cache.getRaw('users');
139
+ * if (raw) {
140
+ * await serviceB.forward('/api/users/sync', raw);
141
+ * }
142
+ * ```
143
+ */
144
+ declare function createTerseServiceClient(config: TerseServiceClientConfig): {
145
+ /**
146
+ * GET request - receives TerseJSON and returns Proxy-wrapped (or raw)
147
+ */
148
+ get<T>(path: string, options?: {
149
+ headers?: Record<string, string>;
150
+ }): Promise<T>;
151
+ /**
152
+ * POST request with automatic compression
153
+ */
154
+ post<T, R = unknown>(path: string, data: T, options?: {
155
+ headers?: Record<string, string>;
156
+ compress?: boolean;
157
+ }): Promise<R>;
158
+ /**
159
+ * Forward a raw TersePayload to another endpoint without expansion.
160
+ * Useful for passing data between services without intermediate deserialization.
161
+ */
162
+ forward<T extends Record<string, unknown>[]>(path: string, payload: TersePayload<T>, options?: {
163
+ headers?: Record<string, string>;
164
+ }): Promise<void>;
165
+ };
166
+
167
+ export { CompressOptions, type CompressStreamOptions, TerseCache, type TerseCacheOptions, TersePayload, type TerseServiceClientConfig, compressStream, createTerseServiceClient };