Commit 6a8244d
feat: add User-Agent headers with absolute error handling guarantees
## Summary
Implement industry-standard dual-header approach (User-Agent + X-Envoy-Client-Info)
for client identification, following patterns from Stripe, AWS, OpenAI, and Twilio SDKs.
## ABSOLUTE GUARANTEES
**GUARANTEE 1: SDK initialization NEVER fails due to User-Agent headers**
- All header generation wrapped in triple-nested try-catch blocks
- Primary attempt → Secondary fallback → Tertiary fallback (no headers)
- Each layer independently catches and handles ALL exceptions
**GUARANTEE 2: All failures are SILENT - no customer log pollution**
- Zero console.error calls in production code
- No assumptions about customer environment (NODE_ENV, etc.)
- Errors are completely invisible to SDK users
**GUARANTEE 3: Meaningful fallbacks at every layer**
- package.json version fails → 'unknown'
- process.version fails → 'unknown'
- os.platform() fails → 'unknown'
- JSON.stringify fails → hardcoded valid JSON string
- Header setting fails → minimal valid headers or no headers
**GUARANTEE 4: User-Agent is telemetry, not critical functionality**
- Authorization header (critical) is NOT wrapped in error handling
- User-Agent headers (telemetry) are completely best-effort
- SDK remains 100% functional without User-Agent headers
## Headers Implemented
### 1. Standard User-Agent (Universal Compatibility)
```
envoy-integrations-sdk/2.4.4 node/18.0.0 [CustomApp/1.0.0]
```
### 2. X-Envoy-Client-Info (Rich Telemetry - JSON)
```json
{
"sdk": "envoy-integrations-sdk",
"version": "2.4.4",
"runtime": "node",
"runtimeVersion": "18.0.0",
"platform": "darwin",
"application": "CustomApp/1.0.0"
}
```
## Error Handling Architecture
### Layer 1: Helper Functions
- `getNodeVersion()`: Returns 'unknown' on any error
- `getPlatform()`: Returns 'unknown' on any error
- Module-level version loading: Defaults to 'unknown'
### Layer 2: Build Functions
- `buildUserAgent()`: Try-catch → Returns 'envoy-integrations-sdk/unknown node/unknown'
- `buildClientInfo()`: Try-catch → Returns minimal ClientInfo object
- `buildClientInfoHeader()`: Try-catch → Returns hardcoded valid JSON string
### Layer 3: Constructor
- Primary: Call build functions
- Secondary: Set minimal fallback headers ('envoy-integrations-sdk/unknown')
- Tertiary: Continue without headers if even fallbacks fail
## Error Scenarios Covered
✅ package.json not found or unreadable
✅ package.json.version missing or malformed
✅ process.version throws exception
✅ process.version missing/undefined
✅ process.version.replace() fails
✅ os.platform() throws exception
✅ JSON.stringify() fails
✅ customUserAgent malformed or contains non-ASCII
✅ axios.defaults.headers assignment fails
✅ Multiple simultaneous failures
✅ Unknown/future edge cases (caught by outer try-catch)
## Implementation Details
### New Files
- `src/util/userAgent.ts` (145 lines)
- buildUserAgent() - never throws
- buildClientInfo() - never throws
- buildClientInfoHeader() - never throws
- getNodeVersion() - never throws
- getPlatform() - never throws
### Modified Files
- `src/base/EnvoyAPI.ts`
- Constructor accepts EnvoyAPIOptions | string (backward compatible)
- Triple-nested error handling for header setting
- Headers set automatically with meaningful fallbacks
- `src/index.ts`
- Export EnvoyAPIOptions type
- Export userAgent utility functions
### New Test Files
- `test/util/userAgent.test.ts` (19 tests)
- `test/base/EnvoyAPI.test.ts` (29 tests)
## Test Coverage
**62 tests total - all passing ✅**
### userAgent utilities (19 tests)
- buildUserAgent with/without custom app
- buildClientInfo with different platforms
- buildClientInfoHeader JSON validation
- Error handling (missing process.version, os.platform errors)
- Edge cases (empty strings, special characters)
- Functions never throw guarantee
### EnvoyAPI constructor (29 tests)
- Backward compatibility (string parameter)
- New options object parameter
- Header setting verification
- Format validation (regex, JSON structure)
- Error resilience (SDK initialization succeeds despite errors)
- Authorization header always succeeds
- Fallback headers used on errors
### Existing tests (14 tests)
- All existing axiosConstructor tests pass
- No regressions introduced
## Usage
### Legacy (Still Works) ✅
```typescript
const client = new EnvoyUserAPI('access-token');
// Headers automatically set with defaults
```
### New (Optional Custom Identifier)
```typescript
const client = new EnvoyUserAPI({
accessToken: 'access-token',
userAgent: 'AcmePortal/2.1.0'
});
// Headers include custom application identifier
```
## Backward Compatibility
✅ 100% backward compatible
✅ Zero breaking changes
✅ Existing code works unchanged
✅ String constructor still supported
✅ All child classes (EnvoyUserAPI, EnvoyPluginAPI) inherit compatibility
## Industry Research
Analyzed User-Agent patterns from:
- **Stripe SDK**: appInfo + JSON-encoded metadata
- **AWS SDK v3**: Middleware-based with customizable provider
- **OpenAI SDK**: defaultHeaders configuration
- **Twilio SDK**: userAgentExtensions parameter
Our dual-header approach combines best practices from all four.
## Benefits
### For Envoy
- Track SDK version adoption
- Debug customer issues faster
- Platform analytics (Node.js versions, OS distribution)
### For Customers
- Identify applications in API usage
- Better support with full context
- Industry-standard pattern (familiar)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>1 parent 5445f76 commit 6a8244d
File tree
5 files changed
+893
-1
lines changed- src
- base
- util
- test
- base
- util
5 files changed
+893
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
12 | 29 | | |
13 | 30 | | |
14 | 31 | | |
| |||
27 | 44 | | |
28 | 45 | | |
29 | 46 | | |
| 47 | + | |
30 | 48 | | |
31 | 49 | | |
32 | 50 | | |
| |||
59 | 77 | | |
60 | 78 | | |
61 | 79 | | |
62 | | - | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
63 | 104 | | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
64 | 125 | | |
65 | 126 | | |
66 | 127 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
65 | 68 | | |
66 | 69 | | |
67 | 70 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
0 commit comments