@oncely/core 0.2.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,183 @@
1
+ import { O as OncelyConfig, S as StorageAdapter, c as createInstance } from './errors-BUehgS6t.cjs';
2
+ export { A as AcquireResult, C as ConflictError, I as IdempotencyError, M as MemoryStorage, d as MismatchError, b as MissingKeyError, k as OnConflictCallback, l as OnErrorCallback, i as OnHitCallback, j as OnMissCallback, a as Oncely, g as OncelyOptions, P as ProblemDetails, R as RunOptions, h as RunResult, e as StorageError, f as StoredResponse } from './errors-BUehgS6t.cjs';
3
+
4
+ type GlobalConfig = OncelyConfig;
5
+ /**
6
+ * Configure global defaults for oncely.
7
+ * Call this once at application startup to set defaults for all oncely operations.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { oncely } from '@oncely/core';
12
+ * import { redis } from '@oncely/redis';
13
+ *
14
+ * // Set up once at app startup
15
+ * oncely.configure({
16
+ * storage: redis(),
17
+ * ttl: '1h',
18
+ * onHit: (key) => console.log(`Cache hit: ${key}`),
19
+ * });
20
+ * ```
21
+ */
22
+ declare function configure(config: OncelyConfig): void;
23
+ /**
24
+ * Get the current global configuration.
25
+ */
26
+ declare function getConfig(): OncelyConfig;
27
+ /**
28
+ * Reset global configuration to defaults.
29
+ * Useful for testing.
30
+ */
31
+ declare function resetConfig(): void;
32
+ /**
33
+ * Get the default storage adapter.
34
+ * Returns the configured global storage, or a shared memory instance.
35
+ */
36
+ declare function getDefaultStorage(): StorageAdapter;
37
+
38
+ /**
39
+ * HTTP header constants following IETF draft-ietf-httpapi-idempotency-key-header.
40
+ * @see https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-idempotency-key-header
41
+ */
42
+ /** Standard header name for idempotency keys */
43
+ declare const HEADER = "Idempotency-Key";
44
+ /** Header indicating a response was replayed from cache */
45
+ declare const HEADER_REPLAY = "Idempotency-Replay";
46
+
47
+ /**
48
+ * Parse a TTL string into milliseconds.
49
+ * Supports: '30s', '5m', '24h', '7d'
50
+ */
51
+ declare function parseTtl(ttl: number | string): number;
52
+ /**
53
+ * Generate a simple hash from a string using DJB2 algorithm.
54
+ *
55
+ * **Note:** This is a fast, non-cryptographic hash suitable for request
56
+ * fingerprinting. It has a 32-bit output space (~4 billion unique values).
57
+ *
58
+ * **Collision Risk:** At ~77,000 unique payloads, there's a 50% probability
59
+ * of collision (birthday problem). For high-volume payment systems or
60
+ * security-sensitive applications, use {@link hashObjectSecure} instead.
61
+ *
62
+ * For typical API use cases with moderate volume, DJB2 provides excellent
63
+ * performance (~2.4M ops/sec vs ~600K ops/sec for SHA-256).
64
+ */
65
+ declare function simpleHash(str: string): string;
66
+ /**
67
+ * Generate a SHA-256 hash from a string.
68
+ *
69
+ * Use this for security-sensitive applications where collision resistance
70
+ * is important. Slower than {@link simpleHash} but cryptographically secure.
71
+ */
72
+ declare function secureHash(str: string): string;
73
+ /**
74
+ * Hash an object by JSON stringifying it.
75
+ *
76
+ * Uses the fast DJB2 algorithm (~2.4M ops/sec). For high-volume payment
77
+ * systems or security-sensitive applications where collision resistance
78
+ * is critical, use {@link hashObjectSecure} instead.
79
+ *
80
+ * **Collision Risk:** At ~77,000 unique payloads, there's a 50% probability
81
+ * of collision. A collision could cause:
82
+ * - False mismatch errors (if hashes differ)
83
+ * - Or incorrectly matching different payloads (if hashes collide)
84
+ *
85
+ * **Note:** Object key order affects the hash. If you need order-independent
86
+ * hashing, sort the keys before passing to this function.
87
+ */
88
+ declare function hashObject(obj: unknown): string;
89
+ /**
90
+ * Hash an object using SHA-256.
91
+ *
92
+ * Use this for security-sensitive applications where collision resistance
93
+ * is important. Slower than {@link hashObject} but cryptographically secure.
94
+ */
95
+ declare function hashObjectSecure(obj: unknown): string;
96
+ /**
97
+ * Generate a cryptographically secure unique key (UUID v4).
98
+ *
99
+ * Uses Node.js crypto.randomUUID() for secure random generation.
100
+ */
101
+ declare function generateKey(): string;
102
+ /**
103
+ * Compose a deterministic key from multiple parts.
104
+ */
105
+ declare function composeKey(...parts: (string | number)[]): string;
106
+
107
+ /**
108
+ * Oncely namespace object.
109
+ * All oncely functionality is accessed through this namespace.
110
+ *
111
+ * @example
112
+ * ```typescript
113
+ * import { oncely } from '@oncely/core';
114
+ *
115
+ * // Configure globally
116
+ * oncely.configure({
117
+ * storage: redis(),
118
+ * ttl: '1h',
119
+ * });
120
+ *
121
+ * // Create an instance
122
+ * const instance = oncely.createInstance();
123
+ * const result = await instance.run({
124
+ * key: 'order-123',
125
+ * handler: () => createOrder(data),
126
+ * });
127
+ * ```
128
+ */
129
+ declare const oncely: {
130
+ /**
131
+ * Configure global defaults for oncely.
132
+ * Call this once at application startup.
133
+ */
134
+ readonly configure: typeof configure;
135
+ /**
136
+ * Get the current global configuration.
137
+ */
138
+ readonly getConfig: typeof getConfig;
139
+ /**
140
+ * Reset global configuration to defaults.
141
+ * Useful for testing.
142
+ */
143
+ readonly resetConfig: typeof resetConfig;
144
+ /**
145
+ * Get the default storage adapter.
146
+ */
147
+ readonly getDefaultStorage: typeof getDefaultStorage;
148
+ /**
149
+ * Create an oncely instance with optional configuration.
150
+ * Uses global config merged with provided options.
151
+ */
152
+ readonly createInstance: typeof createInstance;
153
+ /**
154
+ * Standard header name for idempotency keys.
155
+ * @see https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-idempotency-key-header
156
+ */
157
+ readonly HEADER: "Idempotency-Key";
158
+ /**
159
+ * Header indicating a response was replayed from cache.
160
+ */
161
+ readonly HEADER_REPLAY: "Idempotency-Replay";
162
+ };
163
+ /**
164
+ * Interface for oncely namespace - can be extended via module augmentation.
165
+ */
166
+ interface OncelyNamespace {
167
+ readonly configure: typeof configure;
168
+ readonly getConfig: typeof getConfig;
169
+ readonly resetConfig: typeof resetConfig;
170
+ readonly getDefaultStorage: typeof getDefaultStorage;
171
+ readonly createInstance: typeof createInstance;
172
+ readonly HEADER: typeof HEADER;
173
+ readonly HEADER_REPLAY: typeof HEADER_REPLAY;
174
+ [key: string]: unknown;
175
+ }
176
+
177
+ /**
178
+ * Pre-configured memory storage instance.
179
+ * Use this for quick setup in development.
180
+ */
181
+ declare const memory: StorageAdapter;
182
+
183
+ export { type GlobalConfig, HEADER, HEADER_REPLAY, OncelyConfig, type OncelyNamespace, StorageAdapter, composeKey, configure, generateKey, getConfig, getDefaultStorage, hashObject, hashObjectSecure, memory, oncely, parseTtl, resetConfig, secureHash, simpleHash };
@@ -0,0 +1,183 @@
1
+ import { O as OncelyConfig, S as StorageAdapter, c as createInstance } from './errors-BUehgS6t.js';
2
+ export { A as AcquireResult, C as ConflictError, I as IdempotencyError, M as MemoryStorage, d as MismatchError, b as MissingKeyError, k as OnConflictCallback, l as OnErrorCallback, i as OnHitCallback, j as OnMissCallback, a as Oncely, g as OncelyOptions, P as ProblemDetails, R as RunOptions, h as RunResult, e as StorageError, f as StoredResponse } from './errors-BUehgS6t.js';
3
+
4
+ type GlobalConfig = OncelyConfig;
5
+ /**
6
+ * Configure global defaults for oncely.
7
+ * Call this once at application startup to set defaults for all oncely operations.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { oncely } from '@oncely/core';
12
+ * import { redis } from '@oncely/redis';
13
+ *
14
+ * // Set up once at app startup
15
+ * oncely.configure({
16
+ * storage: redis(),
17
+ * ttl: '1h',
18
+ * onHit: (key) => console.log(`Cache hit: ${key}`),
19
+ * });
20
+ * ```
21
+ */
22
+ declare function configure(config: OncelyConfig): void;
23
+ /**
24
+ * Get the current global configuration.
25
+ */
26
+ declare function getConfig(): OncelyConfig;
27
+ /**
28
+ * Reset global configuration to defaults.
29
+ * Useful for testing.
30
+ */
31
+ declare function resetConfig(): void;
32
+ /**
33
+ * Get the default storage adapter.
34
+ * Returns the configured global storage, or a shared memory instance.
35
+ */
36
+ declare function getDefaultStorage(): StorageAdapter;
37
+
38
+ /**
39
+ * HTTP header constants following IETF draft-ietf-httpapi-idempotency-key-header.
40
+ * @see https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-idempotency-key-header
41
+ */
42
+ /** Standard header name for idempotency keys */
43
+ declare const HEADER = "Idempotency-Key";
44
+ /** Header indicating a response was replayed from cache */
45
+ declare const HEADER_REPLAY = "Idempotency-Replay";
46
+
47
+ /**
48
+ * Parse a TTL string into milliseconds.
49
+ * Supports: '30s', '5m', '24h', '7d'
50
+ */
51
+ declare function parseTtl(ttl: number | string): number;
52
+ /**
53
+ * Generate a simple hash from a string using DJB2 algorithm.
54
+ *
55
+ * **Note:** This is a fast, non-cryptographic hash suitable for request
56
+ * fingerprinting. It has a 32-bit output space (~4 billion unique values).
57
+ *
58
+ * **Collision Risk:** At ~77,000 unique payloads, there's a 50% probability
59
+ * of collision (birthday problem). For high-volume payment systems or
60
+ * security-sensitive applications, use {@link hashObjectSecure} instead.
61
+ *
62
+ * For typical API use cases with moderate volume, DJB2 provides excellent
63
+ * performance (~2.4M ops/sec vs ~600K ops/sec for SHA-256).
64
+ */
65
+ declare function simpleHash(str: string): string;
66
+ /**
67
+ * Generate a SHA-256 hash from a string.
68
+ *
69
+ * Use this for security-sensitive applications where collision resistance
70
+ * is important. Slower than {@link simpleHash} but cryptographically secure.
71
+ */
72
+ declare function secureHash(str: string): string;
73
+ /**
74
+ * Hash an object by JSON stringifying it.
75
+ *
76
+ * Uses the fast DJB2 algorithm (~2.4M ops/sec). For high-volume payment
77
+ * systems or security-sensitive applications where collision resistance
78
+ * is critical, use {@link hashObjectSecure} instead.
79
+ *
80
+ * **Collision Risk:** At ~77,000 unique payloads, there's a 50% probability
81
+ * of collision. A collision could cause:
82
+ * - False mismatch errors (if hashes differ)
83
+ * - Or incorrectly matching different payloads (if hashes collide)
84
+ *
85
+ * **Note:** Object key order affects the hash. If you need order-independent
86
+ * hashing, sort the keys before passing to this function.
87
+ */
88
+ declare function hashObject(obj: unknown): string;
89
+ /**
90
+ * Hash an object using SHA-256.
91
+ *
92
+ * Use this for security-sensitive applications where collision resistance
93
+ * is important. Slower than {@link hashObject} but cryptographically secure.
94
+ */
95
+ declare function hashObjectSecure(obj: unknown): string;
96
+ /**
97
+ * Generate a cryptographically secure unique key (UUID v4).
98
+ *
99
+ * Uses Node.js crypto.randomUUID() for secure random generation.
100
+ */
101
+ declare function generateKey(): string;
102
+ /**
103
+ * Compose a deterministic key from multiple parts.
104
+ */
105
+ declare function composeKey(...parts: (string | number)[]): string;
106
+
107
+ /**
108
+ * Oncely namespace object.
109
+ * All oncely functionality is accessed through this namespace.
110
+ *
111
+ * @example
112
+ * ```typescript
113
+ * import { oncely } from '@oncely/core';
114
+ *
115
+ * // Configure globally
116
+ * oncely.configure({
117
+ * storage: redis(),
118
+ * ttl: '1h',
119
+ * });
120
+ *
121
+ * // Create an instance
122
+ * const instance = oncely.createInstance();
123
+ * const result = await instance.run({
124
+ * key: 'order-123',
125
+ * handler: () => createOrder(data),
126
+ * });
127
+ * ```
128
+ */
129
+ declare const oncely: {
130
+ /**
131
+ * Configure global defaults for oncely.
132
+ * Call this once at application startup.
133
+ */
134
+ readonly configure: typeof configure;
135
+ /**
136
+ * Get the current global configuration.
137
+ */
138
+ readonly getConfig: typeof getConfig;
139
+ /**
140
+ * Reset global configuration to defaults.
141
+ * Useful for testing.
142
+ */
143
+ readonly resetConfig: typeof resetConfig;
144
+ /**
145
+ * Get the default storage adapter.
146
+ */
147
+ readonly getDefaultStorage: typeof getDefaultStorage;
148
+ /**
149
+ * Create an oncely instance with optional configuration.
150
+ * Uses global config merged with provided options.
151
+ */
152
+ readonly createInstance: typeof createInstance;
153
+ /**
154
+ * Standard header name for idempotency keys.
155
+ * @see https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-idempotency-key-header
156
+ */
157
+ readonly HEADER: "Idempotency-Key";
158
+ /**
159
+ * Header indicating a response was replayed from cache.
160
+ */
161
+ readonly HEADER_REPLAY: "Idempotency-Replay";
162
+ };
163
+ /**
164
+ * Interface for oncely namespace - can be extended via module augmentation.
165
+ */
166
+ interface OncelyNamespace {
167
+ readonly configure: typeof configure;
168
+ readonly getConfig: typeof getConfig;
169
+ readonly resetConfig: typeof resetConfig;
170
+ readonly getDefaultStorage: typeof getDefaultStorage;
171
+ readonly createInstance: typeof createInstance;
172
+ readonly HEADER: typeof HEADER;
173
+ readonly HEADER_REPLAY: typeof HEADER_REPLAY;
174
+ [key: string]: unknown;
175
+ }
176
+
177
+ /**
178
+ * Pre-configured memory storage instance.
179
+ * Use this for quick setup in development.
180
+ */
181
+ declare const memory: StorageAdapter;
182
+
183
+ export { type GlobalConfig, HEADER, HEADER_REPLAY, OncelyConfig, type OncelyNamespace, StorageAdapter, composeKey, configure, generateKey, getConfig, getDefaultStorage, hashObject, hashObjectSecure, memory, oncely, parseTtl, resetConfig, secureHash, simpleHash };