Skip to content

Commit 14ada06

Browse files
committed
feat: add Valkey caching layer with New Relic monitoring and structured logging
- Add Valkey/Redis cluster caching with automatic failover - Implement cache utilities: getCache, setCache, deleteCache, deleteCachePattern - Fix cluster SCAN to query all master nodes, not just one - Add batch deletion (100 keys) to prevent overwhelming cluster - Centralized cache keys and TTL configuration - Implement standardized logging with New Relic APM integration - Create Logger class with structured JSON logging - Add specialized methods: httpRequest, cacheOperation, businessEvent, securityEvent - Automatic metrics recording (response time, cache hit/miss, status codes) - Replace all console.log/error with logger for consistency - Configure rate limiting via environment variables - HTTP API: HTTP_RATE_LIMIT_WINDOW_MS, HTTP_RATE_LIMIT_MAX_REQUESTS - File uploads: HTTP_FILE_RATE_LIMIT_MAX - Code execution: CODE_EXEC_RATE_LIMIT_MAX, CODE_EXEC_RATE_WINDOW_MS - WebSocket: existing WS_* variables - Log rate limit configuration on startup - Improve security and configuration - Remove default CORS origins (requires explicit configuration) - Add warning when CORS_ORIGINS not set - Set New Relic log level: warn (dev), info (production) - Add graceful shutdown with cache cleanup - Add ESLint + Prettier integration - Install @eslint/js, eslint-config-prettier, eslint-plugin-prettier - Create .prettierrc and .prettierignore - Format all code consistently (double quotes, semicolons, 100 char width) - Fix TypeScript strict mode compliance - Add nullish coalescing operators for optional properties - Proper error type handling throughout - Type-safe LogMetadata for New Relic API compatibility
1 parent 136ad40 commit 14ada06

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1836
-1563
lines changed

.env.example

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ DATABASE_URL=postgresql://postgres:devpassword@localhost:5432/typelets_local
1717
# For production (example):
1818
# DATABASE_URL=postgresql://username:password@hostname:5432/database?sslmode=require
1919

20+
# Valkey Cache (OPTIONAL - for performance optimization)
21+
# VALKEY_HOST=your-cluster.serverless.use1.cache.amazonaws.com
22+
# VALKEY_PORT=6379
23+
2024
# Clerk Authentication (REQUIRED)
2125
# Get your secret key from: https://dashboard.clerk.com/
2226
# For development, use test keys (sk_test_...)
@@ -26,6 +30,7 @@ CLERK_SECRET_KEY=sk_test_your_actual_clerk_secret_key_from_dashboard
2630
# CORS Origins (REQUIRED)
2731
# Comma-separated list of allowed origins (no spaces after commas)
2832
# Include all frontend domains that will access this API
33+
# If not set, all cross-origin requests will be blocked
2934
CORS_ORIGINS=http://localhost:5173,http://localhost:3000
3035
# Production example:
3136
# CORS_ORIGINS=https://app.typelets.com,https://typelets.com
@@ -61,10 +66,10 @@ MESSAGE_AUTH_SECRET=your-very-secure-random-string-here-min-32-chars
6166
# HTTP_FILE_RATE_LIMIT_MAX=100 # Max file operations per window
6267

6368
# WebSocket Rate Limiting
64-
WS_RATE_LIMIT_WINDOW_MS=60000 # Time window: 1 minute (in milliseconds)
65-
WS_RATE_LIMIT_MAX_MESSAGES=300 # Max messages per window per connection
66-
WS_MAX_CONNECTIONS_PER_USER=20 # Max concurrent connections per user
67-
WS_AUTH_TIMEOUT_MS=30000 # Authentication timeout: 30 seconds
69+
WS_RATE_LIMIT_WINDOW_MS=60000# Time window: 1 minute (in milliseconds)
70+
WS_RATE_LIMIT_MAX_MESSAGES=300# Max messages per window per connection
71+
WS_MAX_CONNECTIONS_PER_USER=20# Max concurrent connections per user
72+
WS_AUTH_TIMEOUT_MS=30000# Authentication timeout: 30 seconds
6873

6974
# Production Rate Limiting Recommendations:
7075
# - HTTP: 500-1000 requests per 15 minutes per user
@@ -77,7 +82,7 @@ WS_AUTH_TIMEOUT_MS=30000 # Authentication timeout: 30 seconds
7782
# ================================================================
7883

7984
# File Size Limits
80-
MAX_FILE_SIZE_MB=50 # Maximum size per file (default: 50MB)
85+
MAX_FILE_SIZE_MB=50# Maximum size per file (default: 50MB)
8186
# MAX_NOTE_SIZE_MB=1024 # Maximum total attachments per note (default: 1GB)
8287

8388
# Allowed File Types (handled in code, documented here)
@@ -102,8 +107,8 @@ MAX_FILE_SIZE_MB=50 # Maximum size per file (default: 50MB)
102107
# ================================================================
103108

104109
# Free Tier Limits
105-
FREE_TIER_STORAGE_GB=1 # Storage limit for free users (default: 1GB)
106-
FREE_TIER_NOTE_LIMIT=100 # Note count limit for free users (default: 100)
110+
FREE_TIER_STORAGE_GB=1# Storage limit for free users (default: 1GB)
111+
FREE_TIER_NOTE_LIMIT=100# Note count limit for free users (default: 100)
107112

108113
# Usage tracking for billing analytics
109114
# These limits trigger billing events logged to console
@@ -113,6 +118,12 @@ FREE_TIER_NOTE_LIMIT=100 # Note count limit for free users (default
113118
# LOGGING & MONITORING
114119
# ================================================================
115120

121+
# New Relic APM (OPTIONAL - for production monitoring)
122+
# Get your license key from: https://one.newrelic.com
123+
# NEW_RELIC_APP_NAME=Typelets API
124+
# NEW_RELIC_LICENSE_KEY=your_license_key_here
125+
# NEW_RELIC_LOG_LEVEL=warn # Options: error, warn, info, debug, trace (default: warn in dev, info in prod)
126+
116127
# Debug Logging
117128
# DEBUG=false # Set to true for verbose logging (not recommended in production)
118129

.github/workflows/ci.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,29 @@ on:
99
jobs:
1010
test:
1111
runs-on: ubuntu-latest
12-
12+
1313
strategy:
1414
matrix:
1515
node-version: [20.x, 22.x]
16-
16+
1717
steps:
1818
- uses: actions/checkout@v4
19-
19+
2020
- name: Use Node.js ${{ matrix.node-version }}
2121
uses: actions/setup-node@v4
2222
with:
2323
node-version: ${{ matrix.node-version }}
24-
24+
2525
- name: Setup pnpm
2626
uses: pnpm/action-setup@v4
2727
with:
2828
version: 9.15.0
29-
29+
3030
- name: Install dependencies
3131
run: pnpm install
32-
32+
3333
- name: Run linting
3434
run: pnpm run lint
35-
35+
3636
- name: Run build
37-
run: NODE_OPTIONS="--max-old-space-size=8192" pnpm run build
37+
run: NODE_OPTIONS="--max-old-space-size=8192" pnpm run build

.github/workflows/codeql.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
pull_request:
77
branches: [main]
88
schedule:
9-
- cron: '0 0 * * 0'
9+
- cron: "0 0 * * 0"
1010

1111
jobs:
1212
analyze:
@@ -20,7 +20,7 @@ jobs:
2020
strategy:
2121
fail-fast: false
2222
matrix:
23-
language: ['typescript']
23+
language: ["typescript"]
2424

2525
steps:
2626
- name: Checkout repository
@@ -34,8 +34,8 @@ jobs:
3434
- name: Setup Node.js
3535
uses: actions/setup-node@v4
3636
with:
37-
node-version: '20'
38-
37+
node-version: "20"
38+
3939
- name: Setup pnpm
4040
uses: pnpm/action-setup@v4
4141
with:
@@ -48,4 +48,4 @@ jobs:
4848
run: NODE_OPTIONS="--max-old-space-size=8192" pnpm run build
4949

5050
- name: Perform CodeQL Analysis
51-
uses: github/codeql-action/analyze@v3
51+
uses: github/codeql-action/analyze@v3
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Change Tracking Marker
2+
on:
3+
release:
4+
types: [published]
5+
6+
jobs:
7+
newrelic:
8+
runs-on: ubuntu-latest
9+
name: New Relic
10+
steps:
11+
# This step builds a var with the release tag value to use later
12+
- name: Set Release Version from Tag
13+
run: echo "RELEASE_VERSION=${{ github.ref_name }}" >> $GITHUB_ENV
14+
# This step creates a new Change Tracking Marker
15+
- name: New Relic Application Deployment Marker
16+
uses: newrelic/deployment-marker-action@v2.3.0
17+
with:
18+
apiKey: ${{ secrets.NEW_RELIC_API_KEY }}
19+
guid: ${{ secrets.NEW_RELIC_DEPLOYMENT_ENTITY_GUID }}
20+
version: "${{ env.RELEASE_VERSION }}"
21+
user: "${{ github.actor }}"

.github/workflows/release.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,34 @@ on:
77
jobs:
88
release:
99
runs-on: ubuntu-latest
10-
10+
1111
permissions:
1212
contents: write
1313
issues: write
1414
pull-requests: write
15-
15+
1616
steps:
1717
- uses: actions/checkout@v4
1818
with:
1919
fetch-depth: 0
20-
20+
2121
- name: Setup Node.js
2222
uses: actions/setup-node@v4
2323
with:
24-
node-version: '20'
25-
24+
node-version: "20"
25+
2626
- name: Setup pnpm
2727
uses: pnpm/action-setup@v4
2828
with:
2929
version: 9.15.0
30-
30+
3131
- name: Install dependencies
3232
run: pnpm install
33-
33+
3434
- name: Build
3535
run: NODE_OPTIONS="--max-old-space-size=8192" pnpm run build
36-
36+
3737
- name: Release
3838
env:
3939
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
40-
run: npx semantic-release
40+
run: npx semantic-release

.prettierignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Dependencies
2+
node_modules/
3+
pnpm-lock.yaml
4+
package-lock.json
5+
yarn.lock
6+
7+
# Build outputs
8+
dist/
9+
build/
10+
.next/
11+
out/
12+
13+
# Cache
14+
.cache/
15+
.turbo/
16+
17+
# Logs
18+
*.log
19+
npm-debug.log*
20+
yarn-debug.log*
21+
yarn-error.log*
22+
23+
# Environment files
24+
.env
25+
.env.local
26+
.env.*.local
27+
28+
# Coverage
29+
coverage/
30+
.nyc_output/
31+
32+
# Misc
33+
.DS_Store
34+
*.pem
35+
36+
# Config files
37+
*.config.js
38+
*.config.mjs

.prettierrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"semi": true,
3+
"trailingComma": "es5",
4+
"singleQuote": false,
5+
"printWidth": 100,
6+
"tabWidth": 2,
7+
"useTabs": false,
8+
"arrowParens": "always",
9+
"endOfLine": "auto"
10+
}

.releaserc.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@
1919
[
2020
"@semantic-release/git",
2121
{
22-
"assets": [
23-
"CHANGELOG.md",
24-
"package.json",
25-
"package-lock.json",
26-
"src/version.ts"
27-
],
22+
"assets": ["CHANGELOG.md", "package.json", "package-lock.json", "src/version.ts"],
2823
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
2924
}
3025
],

CHANGELOG.md

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,68 @@
11
## [1.3.1](https://github.com/typelets/typelets-api/compare/v1.3.0...v1.3.1) (2025-09-25)
22

3-
43
### Bug Fixes
54

6-
* enforce encrypted data validation to prevent plaintext exposure ([0cc6f77](https://github.com/typelets/typelets-api/commit/0cc6f77fcbf9def845e1fae0b871c27f7b1fad95))
5+
- enforce encrypted data validation to prevent plaintext exposure ([0cc6f77](https://github.com/typelets/typelets-api/commit/0cc6f77fcbf9def845e1fae0b871c27f7b1fad95))
76

87
# [1.3.0](https://github.com/typelets/typelets-api/compare/v1.2.0...v1.3.0) (2025-09-23)
98

10-
119
### Features
1210

13-
* add comprehensive Sentry monitoring and fix 429 rate limiting error ([6ba5744](https://github.com/typelets/typelets-api/commit/6ba5744022d075216e8053b6c2127cbb38a4824e))
11+
- add comprehensive Sentry monitoring and fix 429 rate limiting error ([6ba5744](https://github.com/typelets/typelets-api/commit/6ba5744022d075216e8053b6c2127cbb38a4824e))
1412

1513
# [1.2.0](https://github.com/typelets/typelets-api/compare/v1.1.1...v1.2.0) (2025-09-20)
1614

17-
1815
### Features
1916

20-
* **ci:** add Husky pre-commit hooks to prevent CI failures ([346bc2b](https://github.com/typelets/typelets-api/commit/346bc2bcd087d5000b7cc21032561c60baf43dda))
21-
* **ci:** add Husky pre-commit hooks to prevent CI failures ([e2b1017](https://github.com/typelets/typelets-api/commit/e2b1017d1bbb133dd1067ef9234fb463ad89f15d))
22-
* **code:** add secure API proxy for code execution ([8d599b5](https://github.com/typelets/typelets-api/commit/8d599b5c6c72e3ae871a9cf71b9304fb8541828e))
17+
- **ci:** add Husky pre-commit hooks to prevent CI failures ([346bc2b](https://github.com/typelets/typelets-api/commit/346bc2bcd087d5000b7cc21032561c60baf43dda))
18+
- **ci:** add Husky pre-commit hooks to prevent CI failures ([e2b1017](https://github.com/typelets/typelets-api/commit/e2b1017d1bbb133dd1067ef9234fb463ad89f15d))
19+
- **code:** add secure API proxy for code execution ([8d599b5](https://github.com/typelets/typelets-api/commit/8d599b5c6c72e3ae871a9cf71b9304fb8541828e))
2320

2421
## [1.1.1](https://github.com/typelets/typelets-api/compare/v1.1.0...v1.1.1) (2025-09-16)
2522

26-
2723
### Bug Fixes
2824

29-
* make note title field optional for encrypted notes ([ec19a48](https://github.com/typelets/typelets-api/commit/ec19a48954a10eb2a2531c3195fc5fe6b3430d70))
25+
- make note title field optional for encrypted notes ([ec19a48](https://github.com/typelets/typelets-api/commit/ec19a48954a10eb2a2531c3195fc5fe6b3430d70))
3026

3127
# [1.1.0](https://github.com/typelets/typelets-api/compare/v1.0.4...v1.1.0) (2025-09-15)
3228

33-
3429
### Features
3530

36-
* **websocket:** implement real-time sync with HMAC authentication and fix folder moves ([8de85b7](https://github.com/typelets/typelets-api/commit/8de85b7eae38b9af76154e40cdeff53d771f6e92))
31+
- **websocket:** implement real-time sync with HMAC authentication and fix folder moves ([8de85b7](https://github.com/typelets/typelets-api/commit/8de85b7eae38b9af76154e40cdeff53d771f6e92))
3732

3833
## [1.0.4](https://github.com/typelets/typelets-api/compare/v1.0.3...v1.0.4) (2025-09-10)
3934

40-
4135
### Bug Fixes
4236

43-
* bundle API with esbuild to resolve directory import errors ([4644c0e](https://github.com/typelets/typelets-api/commit/4644c0e3d2de2eb5796abab36b931615dc81eead))
37+
- bundle API with esbuild to resolve directory import errors ([4644c0e](https://github.com/typelets/typelets-api/commit/4644c0e3d2de2eb5796abab36b931615dc81eead))
4438

4539
## [1.0.3](https://github.com/typelets/typelets-api/compare/v1.0.2...v1.0.3) (2025-09-10)
4640

47-
4841
### Bug Fixes
4942

50-
* include root-level TypeScript files in esbuild output ([cf9bb4f](https://github.com/typelets/typelets-api/commit/cf9bb4fda0fa19925122b816d0375c88c4f39e05))
43+
- include root-level TypeScript files in esbuild output ([cf9bb4f](https://github.com/typelets/typelets-api/commit/cf9bb4fda0fa19925122b816d0375c88c4f39e05))
5144

5245
## [1.0.2](https://github.com/typelets/typelets-api/compare/v1.0.1...v1.0.2) (2025-09-10)
5346

54-
5547
### Bug Fixes
5648

57-
* replace tsc with esbuild to resolve build hanging issue ([235fce7](https://github.com/typelets/typelets-api/commit/235fce77cdde4e2287fe8b25acc7bcb96deb6ff8))
49+
- replace tsc with esbuild to resolve build hanging issue ([235fce7](https://github.com/typelets/typelets-api/commit/235fce77cdde4e2287fe8b25acc7bcb96deb6ff8))
5850

5951
## [1.0.1](https://github.com/typelets/typelets-api/compare/v1.0.0...v1.0.1) (2025-09-10)
6052

61-
6253
### Bug Fixes
6354

64-
* update Dockerfile to use pnpm instead of npm ([13e9639](https://github.com/typelets/typelets-api/commit/13e963965c7e5fa0e060ba8a0d8995eee761620b))
55+
- update Dockerfile to use pnpm instead of npm ([13e9639](https://github.com/typelets/typelets-api/commit/13e963965c7e5fa0e060ba8a0d8995eee761620b))
6556

6657
# 1.0.0 (2025-09-09)
6758

68-
6959
### Bug Fixes
7060

71-
* remove ES module configuration to fix semantic-release scripts ([f869d14](https://github.com/typelets/typelets-api/commit/f869d14cf42b35d119d11e3e25daff98060b7129))
72-
61+
- remove ES module configuration to fix semantic-release scripts ([f869d14](https://github.com/typelets/typelets-api/commit/f869d14cf42b35d119d11e3e25daff98060b7129))
7362

7463
### Features
7564

76-
* initial open source release of Typelets API ([66a3d30](https://github.com/typelets/typelets-api/commit/66a3d30dcbc0a33c4118c6948d9537e885298039))
65+
- initial open source release of Typelets API ([66a3d30](https://github.com/typelets/typelets-api/commit/66a3d30dcbc0a33c4118c6948d9537e885298039))
7766

7867
# Changelog
7968

0 commit comments

Comments
 (0)