@63klabs/cache-data 1.3.6 → 1.3.7
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/CHANGELOG.md +20 -3
- package/CONTRIBUTING.md +18 -12
- package/package.json +5 -1
- package/src/lib/dao-cache.js +137 -11
- package/AI_CONTEXT.md +0 -863
- package/scripts/README.md +0 -175
- package/scripts/audit-documentation.mjs +0 -856
- package/scripts/review-documentation-files.mjs +0 -406
- package/scripts/setup-dev-environment.sh +0 -59
- package/scripts/verify-example-files.mjs +0 -194
package/AI_CONTEXT.md
DELETED
|
@@ -1,863 +0,0 @@
|
|
|
1
|
-
# AI Development Context & Guidelines for This Repository
|
|
2
|
-
|
|
3
|
-
This document provides essential context for AI coding assistants working within this repository.
|
|
4
|
-
|
|
5
|
-
It outlines architectural principles, constraints, naming conventions, and deployment workflows.
|
|
6
|
-
|
|
7
|
-
All code and infrastructure generated by AI must follow these standards.
|
|
8
|
-
|
|
9
|
-
**CRITICAL**: This is an NPM package (@63klabs/cache-data) used by external applications. Backwards compatibility is paramount. Breaking changes require major version bumps and migration guides.
|
|
10
|
-
|
|
11
|
-
## 1. Purpose of This Repository
|
|
12
|
-
|
|
13
|
-
This repository contains the **@63klabs/cache-data NPM package** - a distributed, serverless data caching solution for AWS Lambda Node.js functions. The package provides:
|
|
14
|
-
|
|
15
|
-
- **Cache Module**: Distributed caching using DynamoDB and S3 with optional in-memory caching
|
|
16
|
-
- **Endpoint Module**: HTTP/HTTPS request handling with built-in retry logic and caching
|
|
17
|
-
- **Tools Module**: Logging, debugging, request handling, AWS integration, and utility functions
|
|
18
|
-
|
|
19
|
-
This package is used in production environments handling over 1 million requests per week.
|
|
20
|
-
|
|
21
|
-
## 2. Critical Constraints
|
|
22
|
-
|
|
23
|
-
### 2.1 Backwards Compatibility (HIGHEST PRIORITY)
|
|
24
|
-
|
|
25
|
-
**NEVER break backwards compatibility without explicit user approval and proper versioning.**
|
|
26
|
-
|
|
27
|
-
- All public APIs must remain stable across minor and patch versions
|
|
28
|
-
- Function signatures cannot change without deprecation warnings
|
|
29
|
-
- Parameter names in JSDoc must match actual function parameters exactly
|
|
30
|
-
- Default behaviors cannot change in ways that affect existing users
|
|
31
|
-
- Environment variable names and behaviors must remain consistent
|
|
32
|
-
- Exported classes, functions, and constants must maintain their interfaces
|
|
33
|
-
|
|
34
|
-
**Before making ANY change to public APIs:**
|
|
35
|
-
1. Check if the function/class is exported in `src/index.js`
|
|
36
|
-
2. Search for usage in tests to understand expected behavior
|
|
37
|
-
3. Consider if existing applications would break
|
|
38
|
-
4. If breaking change is necessary, discuss with user first
|
|
39
|
-
|
|
40
|
-
**Deprecation Process:**
|
|
41
|
-
1. Add `@deprecated` JSDoc tag with migration instructions
|
|
42
|
-
2. Log deprecation warnings when deprecated features are used
|
|
43
|
-
3. Update CHANGELOG.md with deprecation notice
|
|
44
|
-
4. Maintain deprecated functionality for at least one major version
|
|
45
|
-
5. Provide clear migration path in documentation
|
|
46
|
-
|
|
47
|
-
### 2.2 Semantic Versioning
|
|
48
|
-
|
|
49
|
-
This package follows strict semantic versioning (semver):
|
|
50
|
-
|
|
51
|
-
- **MAJOR** (x.0.0): Breaking changes, API changes, removed features
|
|
52
|
-
- **MINOR** (1.x.0): New features, new APIs, backwards-compatible additions
|
|
53
|
-
- **PATCH** (1.3.x): Bug fixes, documentation updates, internal improvements
|
|
54
|
-
|
|
55
|
-
Current version: **1.3.6** (from package.json)
|
|
56
|
-
|
|
57
|
-
**Version Bump Guidelines:**
|
|
58
|
-
- Bug fixes that don't change behavior: PATCH
|
|
59
|
-
- New optional parameters with defaults: MINOR
|
|
60
|
-
- New classes or functions: MINOR
|
|
61
|
-
- Changed function signatures: MAJOR
|
|
62
|
-
- Removed or renamed exports: MAJOR
|
|
63
|
-
- Changed default behaviors: MAJOR
|
|
64
|
-
|
|
65
|
-
### 2.3 Testing Requirements
|
|
66
|
-
|
|
67
|
-
**ALL changes must include appropriate tests. No exceptions.**
|
|
68
|
-
|
|
69
|
-
#### Test Types Required:
|
|
70
|
-
|
|
71
|
-
1. **Unit Tests** (Required for all new functions/classes)
|
|
72
|
-
- Test specific examples and expected behavior
|
|
73
|
-
- Test edge cases (null, undefined, empty, boundary values)
|
|
74
|
-
- Test error conditions and error messages
|
|
75
|
-
- Use descriptive test names
|
|
76
|
-
- Located in `test/` directory matching source structure
|
|
77
|
-
|
|
78
|
-
2. **Property-Based Tests** (Required for core logic)
|
|
79
|
-
- Test universal properties across many inputs
|
|
80
|
-
- Use fast-check library for property generation
|
|
81
|
-
- Validate invariants and mathematical properties
|
|
82
|
-
- Located in `test/*/property/` directories
|
|
83
|
-
|
|
84
|
-
3. **Integration Tests** (Required for module interactions)
|
|
85
|
-
- Test interactions between classes/modules
|
|
86
|
-
- Test AWS service integrations (with mocks)
|
|
87
|
-
- Test end-to-end workflows
|
|
88
|
-
- Located in `test/*/integration/` directories
|
|
89
|
-
|
|
90
|
-
#### Test Execution:
|
|
91
|
-
|
|
92
|
-
```bash
|
|
93
|
-
# Run all tests
|
|
94
|
-
npm test
|
|
95
|
-
|
|
96
|
-
# Run specific test suites
|
|
97
|
-
npm run test:cache
|
|
98
|
-
npm run test:config
|
|
99
|
-
npm run test:endpoint
|
|
100
|
-
npm run test:logging
|
|
101
|
-
npm run test:request
|
|
102
|
-
npm run test:response
|
|
103
|
-
npm run test:utils
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
#### Test Requirements Before Merging:
|
|
107
|
-
|
|
108
|
-
- [ ] All existing tests pass
|
|
109
|
-
- [ ] New tests added for new functionality
|
|
110
|
-
- [ ] Property-based tests added for core logic
|
|
111
|
-
- [ ] Edge cases covered
|
|
112
|
-
- [ ] Error conditions tested
|
|
113
|
-
- [ ] No test coverage regression
|
|
114
|
-
- [ ] Tests are deterministic (no flaky tests)
|
|
115
|
-
|
|
116
|
-
**CRITICAL**: If tests fail, fix the code OR the test. Never ignore failing tests. Tests may reveal bugs in code - don't assume code is always correct.
|
|
117
|
-
|
|
118
|
-
### 2.4 Documentation Requirements
|
|
119
|
-
|
|
120
|
-
**ALL public APIs must have complete JSDoc documentation.**
|
|
121
|
-
|
|
122
|
-
See `.kiro/steering/documentation-standards.md` for complete documentation standards.
|
|
123
|
-
|
|
124
|
-
#### Required JSDoc Tags:
|
|
125
|
-
|
|
126
|
-
**For Functions/Methods:**
|
|
127
|
-
- Description (no tag, just text)
|
|
128
|
-
- `@param` for each parameter with type and description
|
|
129
|
-
- `@returns` with type and description (omit only for void)
|
|
130
|
-
- `@example` with at least one working code example
|
|
131
|
-
- `@throws` for each error type that can be thrown
|
|
132
|
-
|
|
133
|
-
**For Classes:**
|
|
134
|
-
- Description of purpose and responsibilities
|
|
135
|
-
- `@param` for constructor parameters
|
|
136
|
-
- `@example` showing instantiation and common usage
|
|
137
|
-
- `@property` for public properties
|
|
138
|
-
|
|
139
|
-
#### Documentation Update Checklist:
|
|
140
|
-
|
|
141
|
-
- [ ] JSDoc added/updated for all public APIs
|
|
142
|
-
- [ ] Parameter names match function signatures exactly
|
|
143
|
-
- [ ] Return types match actual return values
|
|
144
|
-
- [ ] Examples are executable and correct
|
|
145
|
-
- [ ] User documentation updated if behavior changed
|
|
146
|
-
- [ ] CHANGELOG.md updated with user-facing changes
|
|
147
|
-
- [ ] All documentation validation tests pass
|
|
148
|
-
|
|
149
|
-
#### Run Documentation Validation:
|
|
150
|
-
|
|
151
|
-
```bash
|
|
152
|
-
# Run all documentation tests
|
|
153
|
-
npm test -- test/documentation/
|
|
154
|
-
|
|
155
|
-
# Run documentation audit
|
|
156
|
-
node scripts/audit-documentation.mjs
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
**CRITICAL**: Documentation must be accurate. No hallucinations. Every documented feature must exist in the implementation.
|
|
160
|
-
|
|
161
|
-
## 3. Architecture and Code Organization
|
|
162
|
-
|
|
163
|
-
### 3.1 Module Structure
|
|
164
|
-
|
|
165
|
-
The package is organized into three main modules:
|
|
166
|
-
|
|
167
|
-
```
|
|
168
|
-
src/
|
|
169
|
-
├── index.js # Main entry point, exports tools, cache, endpoint
|
|
170
|
-
├── lib/
|
|
171
|
-
│ ├── dao-cache.js # Cache module (S3Cache, DynamoDbCache, CacheData, Cache)
|
|
172
|
-
│ ├── dao-endpoint.js # Endpoint module (Endpoint class, get function)
|
|
173
|
-
│ ├── tools/ # Tools module
|
|
174
|
-
│ │ ├── index.js # Tools entry point
|
|
175
|
-
│ │ ├── AWS.classes.js # AWS SDK wrappers
|
|
176
|
-
│ │ ├── APIRequest.class.js
|
|
177
|
-
│ │ ├── ClientRequest.class.js
|
|
178
|
-
│ │ ├── Response.class.js
|
|
179
|
-
│ │ ├── ResponseDataModel.class.js
|
|
180
|
-
│ │ ├── Timer.class.js
|
|
181
|
-
│ │ ├── DebugAndLog.class.js
|
|
182
|
-
│ │ ├── ImmutableObject.class.js
|
|
183
|
-
│ │ ├── Connections.classes.js
|
|
184
|
-
│ │ ├── CachedParametersSecrets.classes.js
|
|
185
|
-
│ │ ├── RequestInfo.class.js
|
|
186
|
-
│ │ ├── generic.response.*.js
|
|
187
|
-
│ │ ├── utils.js
|
|
188
|
-
│ │ └── vars.js
|
|
189
|
-
│ └── utils/
|
|
190
|
-
│ └── InMemoryCache.js
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
### 3.2 Separation of Concerns
|
|
194
|
-
|
|
195
|
-
**CRITICAL**: Maintain clear separation between modules and classes.
|
|
196
|
-
|
|
197
|
-
#### Cache Module (`dao-cache.js`):
|
|
198
|
-
- **S3Cache**: Low-level S3 storage operations only
|
|
199
|
-
- **DynamoDbCache**: Low-level DynamoDB storage operations only
|
|
200
|
-
- **CacheData**: Cache management, encryption, expiration logic
|
|
201
|
-
- **Cache**: Public API, cache profiles, configuration
|
|
202
|
-
|
|
203
|
-
**DO NOT**:
|
|
204
|
-
- Add endpoint logic to cache classes
|
|
205
|
-
- Add cache logic to endpoint classes
|
|
206
|
-
- Mix storage operations with business logic
|
|
207
|
-
- Add AWS SDK calls outside of designated classes
|
|
208
|
-
|
|
209
|
-
#### Endpoint Module (`dao-endpoint.js`):
|
|
210
|
-
- **Endpoint**: HTTP request handling and response parsing
|
|
211
|
-
- **get()**: Public API function
|
|
212
|
-
|
|
213
|
-
**DO NOT**:
|
|
214
|
-
- Add cache logic to endpoint classes
|
|
215
|
-
- Add business logic to request handling
|
|
216
|
-
- Mix concerns between modules
|
|
217
|
-
|
|
218
|
-
#### Tools Module (`tools/`):
|
|
219
|
-
- Each class has a single, well-defined responsibility
|
|
220
|
-
- Utility functions are pure and stateless
|
|
221
|
-
- AWS SDK wrappers isolate AWS-specific code
|
|
222
|
-
|
|
223
|
-
**DO NOT**:
|
|
224
|
-
- Create god classes with multiple responsibilities
|
|
225
|
-
- Add business logic to utility functions
|
|
226
|
-
- Mix AWS SDK versions (use v3 only)
|
|
227
|
-
|
|
228
|
-
### 3.3 Class Design Principles
|
|
229
|
-
|
|
230
|
-
1. **Single Responsibility**: Each class does one thing well
|
|
231
|
-
2. **Static vs Instance**: Use static for stateless operations, instance for stateful
|
|
232
|
-
3. **Initialization**: Static classes use `init()` method, called once at boot
|
|
233
|
-
4. **Private Fields**: Use `#` prefix for private static/instance fields
|
|
234
|
-
5. **Immutability**: Prefer immutable objects where possible
|
|
235
|
-
6. **Error Handling**: Always handle errors gracefully, log appropriately
|
|
236
|
-
|
|
237
|
-
### 3.4 Adding New Functionality
|
|
238
|
-
|
|
239
|
-
**Before adding new methods or classes:**
|
|
240
|
-
|
|
241
|
-
1. **Check for existing functionality**
|
|
242
|
-
- Search codebase for similar functionality
|
|
243
|
-
- Check if existing classes can be extended
|
|
244
|
-
- Avoid duplication at all costs
|
|
245
|
-
|
|
246
|
-
2. **Determine proper location**
|
|
247
|
-
- Does it belong in cache, endpoint, or tools?
|
|
248
|
-
- Is it a new class or method on existing class?
|
|
249
|
-
- Should it be public or private?
|
|
250
|
-
|
|
251
|
-
3. **Follow existing patterns**
|
|
252
|
-
- Match naming conventions
|
|
253
|
-
- Use similar parameter structures
|
|
254
|
-
- Follow error handling patterns
|
|
255
|
-
- Maintain consistency with existing code
|
|
256
|
-
|
|
257
|
-
4. **Consider backwards compatibility**
|
|
258
|
-
- Will this break existing code?
|
|
259
|
-
- Can it be added without breaking changes?
|
|
260
|
-
- Does it need a deprecation path?
|
|
261
|
-
|
|
262
|
-
**Example: Adding a new cache method**
|
|
263
|
-
|
|
264
|
-
```javascript
|
|
265
|
-
// GOOD: Follows existing patterns
|
|
266
|
-
/**
|
|
267
|
-
* Clear all expired cache entries from storage.
|
|
268
|
-
*
|
|
269
|
-
* @returns {Promise<{success: boolean, count: number}>} Result with count of cleared entries
|
|
270
|
-
* @example
|
|
271
|
-
* const result = await Cache.clearExpired();
|
|
272
|
-
* console.log(`Cleared ${result.count} expired entries`);
|
|
273
|
-
*/
|
|
274
|
-
static async clearExpired() {
|
|
275
|
-
// Implementation
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
// BAD: Breaks patterns, unclear purpose
|
|
279
|
-
static async doCleanup(opts) {
|
|
280
|
-
// Implementation
|
|
281
|
-
}
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
## 4. Code Quality Standards
|
|
285
|
-
|
|
286
|
-
### 4.1 Code Style
|
|
287
|
-
|
|
288
|
-
- **Language**: JavaScript (Node.js >= 20.0.0)
|
|
289
|
-
- **Indentation**: Tabs (not spaces)
|
|
290
|
-
- **Quotes**: Double quotes for strings
|
|
291
|
-
- **Semicolons**: Required
|
|
292
|
-
- **Line Length**: Reasonable (no strict limit, but keep readable)
|
|
293
|
-
- **Comments**: Use JSDoc for public APIs, inline comments for complex logic
|
|
294
|
-
|
|
295
|
-
### 4.2 Naming Conventions
|
|
296
|
-
|
|
297
|
-
**Classes**: PascalCase
|
|
298
|
-
```javascript
|
|
299
|
-
class CacheData { }
|
|
300
|
-
class S3Cache { }
|
|
301
|
-
class APIRequest { }
|
|
302
|
-
```
|
|
303
|
-
|
|
304
|
-
**Functions/Methods**: camelCase
|
|
305
|
-
```javascript
|
|
306
|
-
async function getData() { }
|
|
307
|
-
static async read(idHash) { }
|
|
308
|
-
```
|
|
309
|
-
|
|
310
|
-
**Constants**: UPPER_SNAKE_CASE
|
|
311
|
-
```javascript
|
|
312
|
-
static PRIVATE = "private";
|
|
313
|
-
static PUBLIC = "public";
|
|
314
|
-
static STATUS_NO_CACHE = "original";
|
|
315
|
-
```
|
|
316
|
-
|
|
317
|
-
**Private Fields**: # prefix
|
|
318
|
-
```javascript
|
|
319
|
-
static #bucket = null;
|
|
320
|
-
#syncedNowTimestampInSeconds = 0;
|
|
321
|
-
```
|
|
322
|
-
|
|
323
|
-
**Parameters**: camelCase
|
|
324
|
-
```javascript
|
|
325
|
-
function processData(idHash, syncedNow, bodyContent) { }
|
|
326
|
-
```
|
|
327
|
-
|
|
328
|
-
### 4.3 Error Handling
|
|
329
|
-
|
|
330
|
-
**ALWAYS handle errors gracefully:**
|
|
331
|
-
|
|
332
|
-
```javascript
|
|
333
|
-
// GOOD: Proper error handling
|
|
334
|
-
try {
|
|
335
|
-
const result = await someOperation();
|
|
336
|
-
return result;
|
|
337
|
-
} catch (error) {
|
|
338
|
-
tools.DebugAndLog.error(`Operation failed: ${error?.message || 'Unknown error'}`, error?.stack);
|
|
339
|
-
return defaultValue;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
// BAD: Unhandled errors
|
|
343
|
-
const result = await someOperation(); // Could throw
|
|
344
|
-
```
|
|
345
|
-
|
|
346
|
-
**Error Logging:**
|
|
347
|
-
- Use `tools.DebugAndLog.error()` for errors
|
|
348
|
-
- Include context (what operation failed)
|
|
349
|
-
- Include error message and stack trace
|
|
350
|
-
- Return sensible defaults on error
|
|
351
|
-
|
|
352
|
-
**Throwing Errors:**
|
|
353
|
-
- Only throw for unrecoverable errors
|
|
354
|
-
- Document with `@throws` JSDoc tag
|
|
355
|
-
- Use descriptive error messages
|
|
356
|
-
- Include context in error message
|
|
357
|
-
|
|
358
|
-
### 4.4 Performance Considerations
|
|
359
|
-
|
|
360
|
-
This package is used in high-traffic Lambda functions. Performance matters.
|
|
361
|
-
|
|
362
|
-
**DO**:
|
|
363
|
-
- Minimize AWS SDK calls
|
|
364
|
-
- Use Promise.all() for parallel operations
|
|
365
|
-
- Cache expensive computations
|
|
366
|
-
- Use in-memory cache when appropriate
|
|
367
|
-
- Minimize JSON.stringify() calls (see existing specs)
|
|
368
|
-
|
|
369
|
-
**DON'T**:
|
|
370
|
-
- Make synchronous blocking calls
|
|
371
|
-
- Create unnecessary objects in loops
|
|
372
|
-
- Perform expensive operations in constructors
|
|
373
|
-
- Make redundant AWS API calls
|
|
374
|
-
|
|
375
|
-
### 4.5 Security Considerations
|
|
376
|
-
|
|
377
|
-
**CRITICAL**: This package handles sensitive data and encryption.
|
|
378
|
-
|
|
379
|
-
**DO**:
|
|
380
|
-
- Encrypt sensitive data using CacheData encryption
|
|
381
|
-
- Use secure random for IVs and keys
|
|
382
|
-
- Validate all inputs
|
|
383
|
-
- Sanitize data before logging
|
|
384
|
-
- Use environment variables for secrets (never hardcode)
|
|
385
|
-
|
|
386
|
-
**DON'T**:
|
|
387
|
-
- Log sensitive data (keys, passwords, PII)
|
|
388
|
-
- Store unencrypted sensitive data
|
|
389
|
-
- Trust user input without validation
|
|
390
|
-
- Expose internal implementation details
|
|
391
|
-
|
|
392
|
-
## 5. Testing Strategy
|
|
393
|
-
|
|
394
|
-
### 5.1 Test Organization
|
|
395
|
-
|
|
396
|
-
Tests mirror source structure:
|
|
397
|
-
|
|
398
|
-
```
|
|
399
|
-
test/
|
|
400
|
-
├── cache/ # Cache module tests
|
|
401
|
-
│ ├── cache-tests.mjs
|
|
402
|
-
│ ├── in-memory-cache/
|
|
403
|
-
│ │ ├── unit/
|
|
404
|
-
│ │ ├── integration/
|
|
405
|
-
│ │ └── property/
|
|
406
|
-
├── config/ # Configuration tests
|
|
407
|
-
├── documentation/ # Documentation validation
|
|
408
|
-
│ └── property/
|
|
409
|
-
├── endpoint/ # Endpoint module tests
|
|
410
|
-
├── logging/ # Logging tests
|
|
411
|
-
├── request/ # Request handling tests
|
|
412
|
-
├── response/ # Response tests
|
|
413
|
-
├── utils/ # Utility tests
|
|
414
|
-
└── helpers/ # Test utilities
|
|
415
|
-
```
|
|
416
|
-
|
|
417
|
-
### 5.2 Test Naming Conventions
|
|
418
|
-
|
|
419
|
-
- Test files: `*-tests.mjs`
|
|
420
|
-
- Property tests: `*-property-tests.mjs`
|
|
421
|
-
- Integration tests: `*-integration-tests.mjs`
|
|
422
|
-
- Unit tests: `*-unit-tests.mjs` or `*-tests.mjs`
|
|
423
|
-
|
|
424
|
-
### 5.3 Test Framework
|
|
425
|
-
|
|
426
|
-
- **Test Runner**: Mocha
|
|
427
|
-
- **Assertions**: Chai (expect style)
|
|
428
|
-
- **Property Testing**: fast-check
|
|
429
|
-
- **Mocking**: Sinon (when necessary)
|
|
430
|
-
- **HTTP Testing**: chai-http
|
|
431
|
-
|
|
432
|
-
### 5.4 Writing Good Tests
|
|
433
|
-
|
|
434
|
-
**DO**:
|
|
435
|
-
- Test one thing per test
|
|
436
|
-
- Use descriptive test names
|
|
437
|
-
- Test edge cases and error conditions
|
|
438
|
-
- Make tests deterministic
|
|
439
|
-
- Keep tests fast
|
|
440
|
-
- Use property-based testing for core logic
|
|
441
|
-
|
|
442
|
-
**DON'T**:
|
|
443
|
-
- Test implementation details
|
|
444
|
-
- Create flaky tests
|
|
445
|
-
- Use real AWS services (mock them)
|
|
446
|
-
- Make tests dependent on each other
|
|
447
|
-
- Skip tests without good reason
|
|
448
|
-
|
|
449
|
-
**Example Test Structure:**
|
|
450
|
-
|
|
451
|
-
```javascript
|
|
452
|
-
import { expect } from 'chai';
|
|
453
|
-
import { Cache } from '../src/lib/dao-cache.js';
|
|
454
|
-
|
|
455
|
-
describe('Cache', () => {
|
|
456
|
-
describe('generateIdHash()', () => {
|
|
457
|
-
it('should generate consistent hash for same input', () => {
|
|
458
|
-
const conn = { host: 'example.com', path: '/api' };
|
|
459
|
-
const hash1 = Cache.generateIdHash(conn);
|
|
460
|
-
const hash2 = Cache.generateIdHash(conn);
|
|
461
|
-
expect(hash1).to.equal(hash2);
|
|
462
|
-
});
|
|
463
|
-
|
|
464
|
-
it('should generate different hashes for different inputs', () => {
|
|
465
|
-
const conn1 = { host: 'example.com', path: '/api' };
|
|
466
|
-
const conn2 = { host: 'example.com', path: '/api2' };
|
|
467
|
-
const hash1 = Cache.generateIdHash(conn1);
|
|
468
|
-
const hash2 = Cache.generateIdHash(conn2);
|
|
469
|
-
expect(hash1).to.not.equal(hash2);
|
|
470
|
-
});
|
|
471
|
-
|
|
472
|
-
it('should handle null input gracefully', () => {
|
|
473
|
-
expect(() => Cache.generateIdHash(null)).to.not.throw();
|
|
474
|
-
});
|
|
475
|
-
});
|
|
476
|
-
});
|
|
477
|
-
```
|
|
478
|
-
|
|
479
|
-
## 6. Kiro Steering Documents
|
|
480
|
-
|
|
481
|
-
This repository uses Kiro for AI-assisted development. Steering documents provide additional context and rules.
|
|
482
|
-
|
|
483
|
-
**Location**: `.kiro/steering/`
|
|
484
|
-
|
|
485
|
-
**Available Steering Documents**:
|
|
486
|
-
|
|
487
|
-
1. **spec-naming-convention.md**: Spec directory naming rules
|
|
488
|
-
- Format: `{version}-{feature-name}`
|
|
489
|
-
- Version from package.json with dots replaced by hyphens
|
|
490
|
-
- Example: `.kiro/specs/1-3-6-in-memory-cache/`
|
|
491
|
-
|
|
492
|
-
2. **documentation-standards.md**: Complete documentation standards
|
|
493
|
-
- JSDoc requirements and templates
|
|
494
|
-
- Documentation update process
|
|
495
|
-
- Quality standards and validation
|
|
496
|
-
- Common pitfalls and solutions
|
|
497
|
-
|
|
498
|
-
**CRITICAL**: Always follow steering document rules. They take precedence over general guidelines.
|
|
499
|
-
|
|
500
|
-
## 7. Spec-Driven Development
|
|
501
|
-
|
|
502
|
-
This project uses spec-driven development for new features.
|
|
503
|
-
|
|
504
|
-
### 7.1 Spec Structure
|
|
505
|
-
|
|
506
|
-
Specs are located in `.kiro/specs/{version}-{feature-name}/`:
|
|
507
|
-
|
|
508
|
-
- `requirements.md`: User stories and acceptance criteria
|
|
509
|
-
- `design.md`: Technical design and correctness properties
|
|
510
|
-
- `tasks.md`: Implementation task list
|
|
511
|
-
|
|
512
|
-
### 7.2 Spec Workflow
|
|
513
|
-
|
|
514
|
-
1. **Requirements**: Define what needs to be built
|
|
515
|
-
2. **Design**: Define how it will be built
|
|
516
|
-
3. **Tasks**: Break down into implementable tasks
|
|
517
|
-
4. **Implementation**: Execute tasks one at a time
|
|
518
|
-
5. **Testing**: Validate against correctness properties
|
|
519
|
-
|
|
520
|
-
### 7.3 Property-Based Testing in Specs
|
|
521
|
-
|
|
522
|
-
Specs include correctness properties that must be validated:
|
|
523
|
-
|
|
524
|
-
- Properties are formal specifications of behavior
|
|
525
|
-
- Properties are tested using property-based testing
|
|
526
|
-
- Properties must pass before feature is complete
|
|
527
|
-
- Properties are linked to requirements
|
|
528
|
-
|
|
529
|
-
**Example Property:**
|
|
530
|
-
|
|
531
|
-
```
|
|
532
|
-
Property 1: Cache Hit Consistency
|
|
533
|
-
For all valid cache keys and data:
|
|
534
|
-
If data is written to cache with key K,
|
|
535
|
-
Then reading from cache with key K returns the same data
|
|
536
|
-
Until the expiration time is reached
|
|
537
|
-
```
|
|
538
|
-
|
|
539
|
-
## 8. Common Patterns and Anti-Patterns
|
|
540
|
-
|
|
541
|
-
### 8.1 Good Patterns
|
|
542
|
-
|
|
543
|
-
**Initialization Pattern:**
|
|
544
|
-
```javascript
|
|
545
|
-
class MyClass {
|
|
546
|
-
static #initialized = false;
|
|
547
|
-
static #config = null;
|
|
548
|
-
|
|
549
|
-
static init(config) {
|
|
550
|
-
if (this.#initialized) {
|
|
551
|
-
tools.DebugAndLog.warn("Already initialized");
|
|
552
|
-
return;
|
|
553
|
-
}
|
|
554
|
-
this.#config = config;
|
|
555
|
-
this.#initialized = true;
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
```
|
|
559
|
-
|
|
560
|
-
**Error Handling Pattern:**
|
|
561
|
-
```javascript
|
|
562
|
-
static async operation() {
|
|
563
|
-
return new Promise(async (resolve) => {
|
|
564
|
-
try {
|
|
565
|
-
const result = await doSomething();
|
|
566
|
-
resolve(result);
|
|
567
|
-
} catch (error) {
|
|
568
|
-
tools.DebugAndLog.error(`Operation failed: ${error?.message}`, error?.stack);
|
|
569
|
-
resolve(defaultValue);
|
|
570
|
-
}
|
|
571
|
-
});
|
|
572
|
-
}
|
|
573
|
-
```
|
|
574
|
-
|
|
575
|
-
**Configuration Pattern:**
|
|
576
|
-
```javascript
|
|
577
|
-
static init(parameters) {
|
|
578
|
-
this.#setting = parameters.setting ||
|
|
579
|
-
process.env.ENV_VAR ||
|
|
580
|
-
defaultValue;
|
|
581
|
-
}
|
|
582
|
-
```
|
|
583
|
-
|
|
584
|
-
### 8.2 Anti-Patterns to Avoid
|
|
585
|
-
|
|
586
|
-
**DON'T: Break backwards compatibility**
|
|
587
|
-
```javascript
|
|
588
|
-
// BAD: Changed parameter order
|
|
589
|
-
static write(body, idHash) { } // Was: write(idHash, body)
|
|
590
|
-
|
|
591
|
-
// GOOD: Add new optional parameter at end
|
|
592
|
-
static write(idHash, body, options = {}) { }
|
|
593
|
-
```
|
|
594
|
-
|
|
595
|
-
**DON'T: Ignore errors**
|
|
596
|
-
```javascript
|
|
597
|
-
// BAD: Silent failure
|
|
598
|
-
try {
|
|
599
|
-
await operation();
|
|
600
|
-
} catch (error) {
|
|
601
|
-
// Nothing
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
// GOOD: Log and handle
|
|
605
|
-
try {
|
|
606
|
-
await operation();
|
|
607
|
-
} catch (error) {
|
|
608
|
-
tools.DebugAndLog.error(`Failed: ${error?.message}`, error?.stack);
|
|
609
|
-
return defaultValue;
|
|
610
|
-
}
|
|
611
|
-
```
|
|
612
|
-
|
|
613
|
-
**DON'T: Create god classes**
|
|
614
|
-
```javascript
|
|
615
|
-
// BAD: Too many responsibilities
|
|
616
|
-
class CacheAndEndpointAndLogging {
|
|
617
|
-
cache() { }
|
|
618
|
-
request() { }
|
|
619
|
-
log() { }
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
// GOOD: Separate concerns
|
|
623
|
-
class Cache { }
|
|
624
|
-
class Endpoint { }
|
|
625
|
-
class Logger { }
|
|
626
|
-
```
|
|
627
|
-
|
|
628
|
-
**DON'T: Duplicate functionality**
|
|
629
|
-
```javascript
|
|
630
|
-
// BAD: Reimplementing existing functionality
|
|
631
|
-
function myHash(data) {
|
|
632
|
-
// Custom hash implementation
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
// GOOD: Use existing functionality
|
|
636
|
-
const hash = tools.hashThisData(data);
|
|
637
|
-
```
|
|
638
|
-
|
|
639
|
-
## 9. Deployment and Versioning
|
|
640
|
-
|
|
641
|
-
### 9.1 Release Process
|
|
642
|
-
|
|
643
|
-
1. Update version in `package.json`
|
|
644
|
-
2. Update `CHANGELOG.md` with changes
|
|
645
|
-
3. Run all tests: `npm test`
|
|
646
|
-
4. Run documentation validation
|
|
647
|
-
5. Commit changes
|
|
648
|
-
6. Create git tag: `git tag v1.3.6`
|
|
649
|
-
7. Push to npm: `npm publish`
|
|
650
|
-
|
|
651
|
-
### 9.2 Changelog Format
|
|
652
|
-
|
|
653
|
-
Follow existing CHANGELOG.md format:
|
|
654
|
-
|
|
655
|
-
```markdown
|
|
656
|
-
## [1.3.6] - 2026-02-01
|
|
657
|
-
|
|
658
|
-
### Added
|
|
659
|
-
- New feature description
|
|
660
|
-
|
|
661
|
-
### Changed
|
|
662
|
-
- Changed behavior description
|
|
663
|
-
|
|
664
|
-
### Fixed
|
|
665
|
-
- Bug fix description
|
|
666
|
-
|
|
667
|
-
### Deprecated
|
|
668
|
-
- Deprecated feature with migration path
|
|
669
|
-
```
|
|
670
|
-
|
|
671
|
-
### 9.3 Breaking Changes
|
|
672
|
-
|
|
673
|
-
**CRITICAL**: Breaking changes require major version bump.
|
|
674
|
-
|
|
675
|
-
Breaking changes include:
|
|
676
|
-
- Removed exports
|
|
677
|
-
- Changed function signatures
|
|
678
|
-
- Changed default behaviors
|
|
679
|
-
- Removed or renamed parameters
|
|
680
|
-
- Changed return types
|
|
681
|
-
- Changed error behaviors
|
|
682
|
-
|
|
683
|
-
**Process for breaking changes:**
|
|
684
|
-
1. Discuss with user first
|
|
685
|
-
2. Create migration guide
|
|
686
|
-
3. Update major version
|
|
687
|
-
4. Document in CHANGELOG.md
|
|
688
|
-
5. Update all documentation
|
|
689
|
-
6. Update all examples
|
|
690
|
-
|
|
691
|
-
## 10. AWS Integration
|
|
692
|
-
|
|
693
|
-
### 10.1 AWS Services Used
|
|
694
|
-
|
|
695
|
-
- **S3**: Large object storage for cache
|
|
696
|
-
- **DynamoDB**: Metadata and small object storage
|
|
697
|
-
- **SSM Parameter Store**: Configuration and secrets
|
|
698
|
-
- **Lambda**: Execution environment (Node.js >= 20.0.0)
|
|
699
|
-
|
|
700
|
-
### 10.2 AWS SDK Usage
|
|
701
|
-
|
|
702
|
-
**CRITICAL**: Use AWS SDK v3 only. Do not mix v2 and v3.
|
|
703
|
-
|
|
704
|
-
```javascript
|
|
705
|
-
// GOOD: Use tools.AWS wrapper
|
|
706
|
-
const result = await tools.AWS.s3.get(params);
|
|
707
|
-
const item = await tools.AWS.dynamo.get(params);
|
|
708
|
-
|
|
709
|
-
// BAD: Direct SDK usage
|
|
710
|
-
const s3 = new S3Client();
|
|
711
|
-
```
|
|
712
|
-
|
|
713
|
-
### 10.3 IAM Permissions Required
|
|
714
|
-
|
|
715
|
-
Lambda execution role needs:
|
|
716
|
-
- S3: GetObject, PutObject
|
|
717
|
-
- DynamoDB: GetItem, PutItem
|
|
718
|
-
- SSM: GetParameter, GetParameters, GetParametersByPath
|
|
719
|
-
|
|
720
|
-
## 11. Performance Optimization
|
|
721
|
-
|
|
722
|
-
### 11.1 Lambda Memory Allocation
|
|
723
|
-
|
|
724
|
-
Recommended: 1024MB - 2048MB
|
|
725
|
-
Minimum: 512MB
|
|
726
|
-
|
|
727
|
-
See: `docs/lambda-optimization/README.md`
|
|
728
|
-
|
|
729
|
-
### 11.2 Cache Strategy
|
|
730
|
-
|
|
731
|
-
- Use in-memory cache for hot data
|
|
732
|
-
- Use DynamoDB for small items (< 10KB)
|
|
733
|
-
- Use S3 for large items (> 10KB)
|
|
734
|
-
- Set appropriate expiration times
|
|
735
|
-
- Use interval-based expiration for predictable patterns
|
|
736
|
-
|
|
737
|
-
### 11.3 Optimization Techniques
|
|
738
|
-
|
|
739
|
-
- Minimize JSON.stringify() calls
|
|
740
|
-
- Use Promise.all() for parallel operations
|
|
741
|
-
- Reuse connections and clients
|
|
742
|
-
- Cache expensive computations
|
|
743
|
-
- Use appropriate data structures
|
|
744
|
-
|
|
745
|
-
## 12. Troubleshooting
|
|
746
|
-
|
|
747
|
-
### 12.1 Common Issues
|
|
748
|
-
|
|
749
|
-
**Issue**: Tests failing after changes
|
|
750
|
-
- **Solution**: Check if backwards compatibility broken
|
|
751
|
-
- **Solution**: Verify test expectations are correct
|
|
752
|
-
- **Solution**: Check for race conditions
|
|
753
|
-
|
|
754
|
-
**Issue**: Documentation validation failing
|
|
755
|
-
- **Solution**: Ensure JSDoc matches implementation
|
|
756
|
-
- **Solution**: Check parameter names match exactly
|
|
757
|
-
- **Solution**: Verify examples are executable
|
|
758
|
-
|
|
759
|
-
**Issue**: Performance degradation
|
|
760
|
-
- **Solution**: Check for unnecessary AWS calls
|
|
761
|
-
- **Solution**: Verify in-memory cache is enabled
|
|
762
|
-
- **Solution**: Check Lambda memory allocation
|
|
763
|
-
|
|
764
|
-
### 12.2 Debugging
|
|
765
|
-
|
|
766
|
-
Use `tools.DebugAndLog` for debugging:
|
|
767
|
-
|
|
768
|
-
```javascript
|
|
769
|
-
tools.DebugAndLog.debug('Debug message', data);
|
|
770
|
-
tools.DebugAndLog.info('Info message');
|
|
771
|
-
tools.DebugAndLog.warn('Warning message');
|
|
772
|
-
tools.DebugAndLog.error('Error message', error?.stack);
|
|
773
|
-
```
|
|
774
|
-
|
|
775
|
-
Set log level via environment variable:
|
|
776
|
-
```bash
|
|
777
|
-
DEBUG_MODE=true
|
|
778
|
-
```
|
|
779
|
-
|
|
780
|
-
## 13. Quick Reference
|
|
781
|
-
|
|
782
|
-
### 13.1 Before Making Changes
|
|
783
|
-
|
|
784
|
-
- [ ] Read this AI_CONTEXT.md document
|
|
785
|
-
- [ ] Review steering documents in `.kiro/steering/`
|
|
786
|
-
- [ ] Check existing tests for expected behavior
|
|
787
|
-
- [ ] Search codebase for similar functionality
|
|
788
|
-
- [ ] Verify backwards compatibility impact
|
|
789
|
-
|
|
790
|
-
### 13.2 Before Committing
|
|
791
|
-
|
|
792
|
-
- [ ] All tests pass: `npm test`
|
|
793
|
-
- [ ] Documentation updated and validated
|
|
794
|
-
- [ ] CHANGELOG.md updated
|
|
795
|
-
- [ ] No breaking changes (or properly versioned)
|
|
796
|
-
- [ ] Code follows style guidelines
|
|
797
|
-
- [ ] Error handling is proper
|
|
798
|
-
- [ ] Performance impact considered
|
|
799
|
-
|
|
800
|
-
### 13.3 Key Commands
|
|
801
|
-
|
|
802
|
-
```bash
|
|
803
|
-
# Run all tests
|
|
804
|
-
npm test
|
|
805
|
-
|
|
806
|
-
# Run specific test suite
|
|
807
|
-
npm run test:cache
|
|
808
|
-
npm run test:endpoint
|
|
809
|
-
npm run test:utils
|
|
810
|
-
|
|
811
|
-
# Run documentation validation
|
|
812
|
-
npm test -- test/documentation/
|
|
813
|
-
|
|
814
|
-
# Run documentation audit
|
|
815
|
-
node scripts/audit-documentation.mjs
|
|
816
|
-
```
|
|
817
|
-
|
|
818
|
-
### 13.4 Key Files
|
|
819
|
-
|
|
820
|
-
- `src/index.js`: Main entry point
|
|
821
|
-
- `src/lib/dao-cache.js`: Cache module
|
|
822
|
-
- `src/lib/dao-endpoint.js`: Endpoint module
|
|
823
|
-
- `src/lib/tools/index.js`: Tools module
|
|
824
|
-
- `package.json`: Version and dependencies
|
|
825
|
-
- `CHANGELOG.md`: Version history
|
|
826
|
-
- `.kiro/steering/`: Steering documents
|
|
827
|
-
|
|
828
|
-
## 14. Getting Help
|
|
829
|
-
|
|
830
|
-
### 14.1 Documentation
|
|
831
|
-
|
|
832
|
-
- README.md: Package overview and quick start
|
|
833
|
-
- docs/: Detailed documentation
|
|
834
|
-
- test/: Test examples showing usage
|
|
835
|
-
- .kiro/steering/: Development guidelines
|
|
836
|
-
|
|
837
|
-
### 14.2 Resources
|
|
838
|
-
|
|
839
|
-
- GitHub Issues: https://github.com/63Klabs/cache-data/issues
|
|
840
|
-
- NPM Package: https://www.npmjs.com/package/@63klabs/cache-data
|
|
841
|
-
- Atlantis Platform: https://github.com/63klabs/atlantis-tutorials
|
|
842
|
-
|
|
843
|
-
---
|
|
844
|
-
|
|
845
|
-
## Summary
|
|
846
|
-
|
|
847
|
-
**Remember the priorities:**
|
|
848
|
-
|
|
849
|
-
1. **Backwards Compatibility**: Never break existing code
|
|
850
|
-
2. **Testing**: All changes must have tests
|
|
851
|
-
3. **Documentation**: All public APIs must be documented
|
|
852
|
-
4. **Separation of Concerns**: Keep modules and classes focused
|
|
853
|
-
5. **Performance**: This runs in production at scale
|
|
854
|
-
6. **Security**: Handle sensitive data properly
|
|
855
|
-
|
|
856
|
-
**When in doubt:**
|
|
857
|
-
- Check existing code for patterns
|
|
858
|
-
- Review steering documents
|
|
859
|
-
- Ask the user before making breaking changes
|
|
860
|
-
- Write tests first
|
|
861
|
-
- Document as you go
|
|
862
|
-
|
|
863
|
-
This package is used in production by real applications. Quality and reliability are non-negotiable.
|