@hivehub/rulebook 2.0.0 → 3.0.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.
Files changed (188) hide show
  1. package/README.md +779 -654
  2. package/dist/cli/commands.d.ts +22 -0
  3. package/dist/cli/commands.d.ts.map +1 -1
  4. package/dist/cli/commands.js +291 -8
  5. package/dist/cli/commands.js.map +1 -1
  6. package/dist/core/claude-mcp.d.ts +32 -0
  7. package/dist/core/claude-mcp.d.ts.map +1 -0
  8. package/dist/core/claude-mcp.js +92 -0
  9. package/dist/core/claude-mcp.js.map +1 -0
  10. package/dist/core/config-manager.d.ts.map +1 -1
  11. package/dist/core/config-manager.js +27 -6
  12. package/dist/core/config-manager.js.map +1 -1
  13. package/dist/core/generator.d.ts.map +1 -1
  14. package/dist/core/generator.js +98 -49
  15. package/dist/core/generator.js.map +1 -1
  16. package/dist/core/migrator.d.ts +13 -0
  17. package/dist/core/migrator.d.ts.map +1 -1
  18. package/dist/core/migrator.js +76 -9
  19. package/dist/core/migrator.js.map +1 -1
  20. package/dist/core/openspec-migrator.d.ts +1 -1
  21. package/dist/core/openspec-migrator.d.ts.map +1 -1
  22. package/dist/core/openspec-migrator.js +14 -7
  23. package/dist/core/openspec-migrator.js.map +1 -1
  24. package/dist/core/workflow-generator.js +297 -176
  25. package/dist/core/workflow-generator.js.map +1 -1
  26. package/dist/index.js +40 -1
  27. package/dist/index.js.map +1 -1
  28. package/dist/mcp/rulebook-server.d.ts.map +1 -1
  29. package/dist/mcp/rulebook-server.js +255 -74
  30. package/dist/mcp/rulebook-server.js.map +1 -1
  31. package/dist/memory/hnsw-index.d.ts +63 -0
  32. package/dist/memory/hnsw-index.d.ts.map +1 -0
  33. package/dist/memory/hnsw-index.js +421 -0
  34. package/dist/memory/hnsw-index.js.map +1 -0
  35. package/dist/memory/memory-cache.d.ts +33 -0
  36. package/dist/memory/memory-cache.d.ts.map +1 -0
  37. package/dist/memory/memory-cache.js +85 -0
  38. package/dist/memory/memory-cache.js.map +1 -0
  39. package/dist/memory/memory-hooks.d.ts +42 -0
  40. package/dist/memory/memory-hooks.d.ts.map +1 -0
  41. package/dist/memory/memory-hooks.js +193 -0
  42. package/dist/memory/memory-hooks.js.map +1 -0
  43. package/dist/memory/memory-manager.d.ts +55 -0
  44. package/dist/memory/memory-manager.d.ts.map +1 -0
  45. package/dist/memory/memory-manager.js +209 -0
  46. package/dist/memory/memory-manager.js.map +1 -0
  47. package/dist/memory/memory-search.d.ts +42 -0
  48. package/dist/memory/memory-search.d.ts.map +1 -0
  49. package/dist/memory/memory-search.js +166 -0
  50. package/dist/memory/memory-search.js.map +1 -0
  51. package/dist/memory/memory-store.d.ts +59 -0
  52. package/dist/memory/memory-store.d.ts.map +1 -0
  53. package/dist/memory/memory-store.js +394 -0
  54. package/dist/memory/memory-store.js.map +1 -0
  55. package/dist/memory/memory-types.d.ts +69 -0
  56. package/dist/memory/memory-types.d.ts.map +1 -0
  57. package/dist/memory/memory-types.js +7 -0
  58. package/dist/memory/memory-types.js.map +1 -0
  59. package/dist/memory/memory-vectorizer.d.ts +29 -0
  60. package/dist/memory/memory-vectorizer.d.ts.map +1 -0
  61. package/dist/memory/memory-vectorizer.js +104 -0
  62. package/dist/memory/memory-vectorizer.js.map +1 -0
  63. package/dist/types.d.ts +7 -0
  64. package/dist/types.d.ts.map +1 -1
  65. package/package.json +107 -106
  66. package/templates/cli/CLAUDE_CODE.md +114 -13
  67. package/templates/commands/rulebook-memory-save.md +48 -0
  68. package/templates/commands/rulebook-memory-search.md +47 -0
  69. package/templates/commands/rulebook-task-apply.md +2 -2
  70. package/templates/commands/rulebook-task-archive.md +2 -2
  71. package/templates/commands/rulebook-task-create.md +2 -2
  72. package/templates/commands/rulebook-task-list.md +2 -2
  73. package/templates/commands/rulebook-task-show.md +2 -2
  74. package/templates/commands/rulebook-task-validate.md +2 -2
  75. package/templates/git/CI_CD_PATTERNS.md +4 -4
  76. package/templates/git/GITHUB_ACTIONS.md +3 -3
  77. package/templates/git/GITLAB_CI.md +4 -4
  78. package/templates/git/SECRETS_MANAGEMENT.md +4 -4
  79. package/templates/hooks/COMMIT_MSG.md +4 -4
  80. package/templates/hooks/POST_CHECKOUT.md +3 -3
  81. package/templates/hooks/PREPARE_COMMIT_MSG.md +3 -3
  82. package/templates/hooks/PRE_COMMIT.md +4 -4
  83. package/templates/hooks/PRE_PUSH.md +4 -4
  84. package/templates/modules/MEMORY.md +63 -0
  85. package/templates/skills/cli/aider/SKILL.md +59 -0
  86. package/templates/skills/cli/amazon-q/SKILL.md +35 -0
  87. package/templates/skills/cli/auggie/SKILL.md +42 -0
  88. package/templates/skills/cli/claude/SKILL.md +42 -0
  89. package/templates/skills/cli/claude-code/SKILL.md +146 -0
  90. package/templates/skills/cli/cline/SKILL.md +42 -0
  91. package/templates/skills/cli/codebuddy/SKILL.md +30 -0
  92. package/templates/skills/cli/codeium/SKILL.md +30 -0
  93. package/templates/skills/cli/codex/SKILL.md +31 -0
  94. package/templates/skills/cli/continue/SKILL.md +44 -0
  95. package/templates/skills/cli/cursor-cli/SKILL.md +38 -0
  96. package/templates/skills/cli/factory/SKILL.md +28 -0
  97. package/templates/skills/cli/gemini/SKILL.md +45 -0
  98. package/templates/skills/cli/kilocode/SKILL.md +28 -0
  99. package/templates/skills/cli/opencode/SKILL.md +28 -0
  100. package/templates/skills/core/agent-automation/SKILL.md +194 -0
  101. package/templates/skills/core/dag/SKILL.md +314 -0
  102. package/templates/skills/core/documentation-rules/SKILL.md +47 -0
  103. package/templates/skills/core/quality-enforcement/SKILL.md +78 -0
  104. package/templates/skills/frameworks/angular/SKILL.md +46 -0
  105. package/templates/skills/frameworks/django/SKILL.md +93 -0
  106. package/templates/skills/frameworks/electron/SKILL.md +157 -0
  107. package/templates/skills/frameworks/flask/SKILL.md +48 -0
  108. package/templates/skills/frameworks/flutter/SKILL.md +65 -0
  109. package/templates/skills/frameworks/jquery/SKILL.md +42 -0
  110. package/templates/skills/frameworks/laravel/SKILL.md +48 -0
  111. package/templates/skills/frameworks/nestjs/SKILL.md +53 -0
  112. package/templates/skills/frameworks/nextjs/SKILL.md +137 -0
  113. package/templates/skills/frameworks/nuxt/SKILL.md +50 -0
  114. package/templates/skills/frameworks/rails/SKILL.md +76 -0
  115. package/templates/skills/frameworks/react/SKILL.md +48 -0
  116. package/templates/skills/frameworks/react-native/SKILL.md +57 -0
  117. package/templates/skills/frameworks/spring/SKILL.md +49 -0
  118. package/templates/skills/frameworks/symfony/SKILL.md +46 -0
  119. package/templates/skills/frameworks/vue/SKILL.md +46 -0
  120. package/templates/skills/frameworks/zend/SKILL.md +45 -0
  121. package/templates/skills/ides/copilot/SKILL.md +47 -0
  122. package/templates/skills/ides/cursor/SKILL.md +53 -0
  123. package/templates/skills/ides/jetbrains-ai/SKILL.md +45 -0
  124. package/templates/skills/ides/replit/SKILL.md +46 -0
  125. package/templates/skills/ides/tabnine/SKILL.md +39 -0
  126. package/templates/skills/ides/vscode/SKILL.md +50 -0
  127. package/templates/skills/ides/windsurf/SKILL.md +46 -0
  128. package/templates/skills/ides/zed/SKILL.md +42 -0
  129. package/templates/skills/languages/ada/SKILL.md +68 -0
  130. package/templates/skills/languages/c/SKILL.md +343 -0
  131. package/templates/skills/languages/cpp/SKILL.md +753 -0
  132. package/templates/skills/languages/csharp/SKILL.md +427 -0
  133. package/templates/skills/languages/dart/SKILL.md +342 -0
  134. package/templates/skills/languages/elixir/SKILL.md +464 -0
  135. package/templates/skills/languages/erlang/SKILL.md +371 -0
  136. package/templates/skills/languages/go/SKILL.md +655 -0
  137. package/templates/skills/languages/haskell/SKILL.md +187 -0
  138. package/templates/skills/languages/java/SKILL.md +617 -0
  139. package/templates/skills/languages/javascript/SKILL.md +641 -0
  140. package/templates/skills/languages/julia/SKILL.md +107 -0
  141. package/templates/skills/languages/kotlin/SKILL.md +521 -0
  142. package/templates/skills/languages/lisp/SKILL.md +110 -0
  143. package/templates/skills/languages/lua/SKILL.md +84 -0
  144. package/templates/skills/languages/objectivec/SKILL.md +100 -0
  145. package/templates/skills/languages/php/SKILL.md +426 -0
  146. package/templates/skills/languages/python/SKILL.md +692 -0
  147. package/templates/skills/languages/r/SKILL.md +360 -0
  148. package/templates/skills/languages/ruby/SKILL.md +431 -0
  149. package/templates/skills/languages/rust/SKILL.md +487 -0
  150. package/templates/skills/languages/sas/SKILL.md +83 -0
  151. package/templates/skills/languages/scala/SKILL.md +358 -0
  152. package/templates/skills/languages/solidity/SKILL.md +590 -0
  153. package/templates/skills/languages/sql/SKILL.md +147 -0
  154. package/templates/skills/languages/swift/SKILL.md +476 -0
  155. package/templates/skills/languages/zig/SKILL.md +275 -0
  156. package/templates/skills/modules/atlassian/SKILL.md +265 -0
  157. package/templates/skills/modules/context7/SKILL.md +64 -0
  158. package/templates/skills/modules/figma/SKILL.md +277 -0
  159. package/templates/skills/modules/github-mcp/SKILL.md +74 -0
  160. package/templates/skills/modules/grafana/SKILL.md +338 -0
  161. package/templates/skills/modules/memory/SKILL.md +73 -0
  162. package/templates/skills/modules/notion/SKILL.md +257 -0
  163. package/templates/skills/modules/playwright/SKILL.md +100 -0
  164. package/templates/skills/modules/rulebook-mcp/SKILL.md +166 -0
  165. package/templates/skills/modules/serena/SKILL.md +347 -0
  166. package/templates/skills/modules/supabase/SKILL.md +233 -0
  167. package/templates/skills/modules/synap/SKILL.md +79 -0
  168. package/templates/skills/modules/vectorizer/SKILL.md +73 -0
  169. package/templates/skills/services/azure-blob/SKILL.md +194 -0
  170. package/templates/skills/services/cassandra/SKILL.md +249 -0
  171. package/templates/skills/services/dynamodb/SKILL.md +318 -0
  172. package/templates/skills/services/elasticsearch/SKILL.md +357 -0
  173. package/templates/skills/services/gcs/SKILL.md +188 -0
  174. package/templates/skills/services/influxdb/SKILL.md +275 -0
  175. package/templates/skills/services/kafka/SKILL.md +351 -0
  176. package/templates/skills/services/mariadb/SKILL.md +193 -0
  177. package/templates/skills/services/memcached/SKILL.md +252 -0
  178. package/templates/skills/services/minio/SKILL.md +211 -0
  179. package/templates/skills/services/mongodb/SKILL.md +278 -0
  180. package/templates/skills/services/mysql/SKILL.md +368 -0
  181. package/templates/skills/services/neo4j/SKILL.md +257 -0
  182. package/templates/skills/services/oracle/SKILL.md +300 -0
  183. package/templates/skills/services/postgresql/SKILL.md +336 -0
  184. package/templates/skills/services/rabbitmq/SKILL.md +296 -0
  185. package/templates/skills/services/redis/SKILL.md +302 -0
  186. package/templates/skills/services/s3/SKILL.md +308 -0
  187. package/templates/skills/services/sqlite/SKILL.md +304 -0
  188. package/templates/skills/services/sqlserver/SKILL.md +304 -0
@@ -0,0 +1,302 @@
1
+ ---
2
+ name: "Redis"
3
+ description: "Use Redis for high-performance caching, session storage, pub/sub messaging, and real-time features."
4
+ version: "1.0.0"
5
+ category: "services"
6
+ author: "Rulebook"
7
+ tags: ["services", "caching"]
8
+ dependencies: []
9
+ conflicts: []
10
+ ---
11
+ <!-- REDIS:START -->
12
+ # Redis Cache Instructions
13
+
14
+ **CRITICAL**: Use Redis for high-performance caching, session storage, pub/sub messaging, and real-time features.
15
+
16
+ ## Core Features
17
+
18
+ ### Connection
19
+ ```typescript
20
+ // Using redis (Node.js)
21
+ import { createClient } from 'redis'
22
+
23
+ const client = createClient({
24
+ url: process.env.REDIS_URL || 'redis://localhost:6379',
25
+ socket: {
26
+ reconnectStrategy: (retries) => {
27
+ if (retries > 10) {
28
+ return new Error('Too many reconnection attempts')
29
+ }
30
+ return Math.min(retries * 100, 3000)
31
+ },
32
+ },
33
+ })
34
+
35
+ await client.connect()
36
+
37
+ // Using ioredis
38
+ import Redis from 'ioredis'
39
+
40
+ const redis = new Redis({
41
+ host: process.env.REDIS_HOST || 'localhost',
42
+ port: parseInt(process.env.REDIS_PORT || '6379'),
43
+ password: process.env.REDIS_PASSWORD,
44
+ retryStrategy: (times) => Math.min(times * 50, 2000),
45
+ maxRetriesPerRequest: 3,
46
+ })
47
+ ```
48
+
49
+ ### Basic Operations
50
+ ```typescript
51
+ // String operations
52
+ await client.set('user:1', JSON.stringify({ name: 'John', email: 'john@example.com' }))
53
+ await client.set('user:1', 'value', { EX: 3600 }) // Expire in 1 hour
54
+ const user = await client.get('user:1')
55
+ await client.del('user:1')
56
+
57
+ // Multiple operations
58
+ await client.mSet({
59
+ 'key1': 'value1',
60
+ 'key2': 'value2',
61
+ })
62
+ const values = await client.mGet(['key1', 'key2'])
63
+
64
+ // Increment/Decrement
65
+ await client.incr('counter')
66
+ await client.incrBy('counter', 5)
67
+ await client.decr('counter')
68
+ ```
69
+
70
+ ### Data Structures
71
+ ```typescript
72
+ // Lists
73
+ await client.lPush('tasks', 'task1', 'task2')
74
+ await client.rPush('tasks', 'task3')
75
+ const task = await client.lPop('tasks')
76
+ const tasks = await client.lRange('tasks', 0, -1)
77
+
78
+ // Sets
79
+ await client.sAdd('tags', 'javascript', 'typescript', 'nodejs')
80
+ const tags = await client.sMembers('tags')
81
+ const exists = await client.sIsMember('tags', 'javascript')
82
+ await client.sRem('tags', 'javascript')
83
+
84
+ // Sorted Sets
85
+ await client.zAdd('leaderboard', {
86
+ score: 100,
87
+ value: 'player1',
88
+ })
89
+ const topPlayers = await client.zRange('leaderboard', 0, 9, { REV: true })
90
+
91
+ // Hashes
92
+ await client.hSet('user:1', {
93
+ name: 'John',
94
+ email: 'john@example.com',
95
+ age: '30',
96
+ })
97
+ const user = await client.hGetAll('user:1')
98
+ await client.hIncrBy('user:1', 'age', 1)
99
+ ```
100
+
101
+ ### Advanced Features
102
+ ```typescript
103
+ // Pub/Sub
104
+ const publisher = client.duplicate()
105
+ await publisher.connect()
106
+
107
+ const subscriber = client.duplicate()
108
+ await subscriber.connect()
109
+
110
+ await subscriber.subscribe('notifications', (message) => {
111
+ console.log('Received:', message)
112
+ })
113
+
114
+ await publisher.publish('notifications', JSON.stringify({ type: 'alert', message: 'Hello' }))
115
+
116
+ // Streams
117
+ await client.xAdd('events', '*', {
118
+ type: 'user_login',
119
+ userId: '123',
120
+ timestamp: Date.now().toString(),
121
+ })
122
+
123
+ const events = await client.xRead({
124
+ key: 'events',
125
+ id: '0',
126
+ }, {
127
+ COUNT: 10,
128
+ BLOCK: 1000,
129
+ })
130
+
131
+ // Transactions
132
+ const multi = client.multi()
133
+ multi.set('key1', 'value1')
134
+ multi.set('key2', 'value2')
135
+ multi.incr('counter')
136
+ await multi.exec()
137
+ ```
138
+
139
+ ## Common Patterns
140
+
141
+ ### Caching
142
+ ```typescript
143
+ async function getCachedUser(userId: string) {
144
+ const cached = await client.get(`user:${userId}`)
145
+ if (cached) {
146
+ return JSON.parse(cached)
147
+ }
148
+
149
+ const user = await fetchUserFromDatabase(userId)
150
+ await client.set(`user:${userId}`, JSON.stringify(user), { EX: 3600 })
151
+ return user
152
+ }
153
+
154
+ // Cache with tags (for invalidation)
155
+ async function cacheWithTags(key: string, value: any, tags: string[], ttl: number) {
156
+ await client.set(key, JSON.stringify(value), { EX: ttl })
157
+ for (const tag of tags) {
158
+ await client.sAdd(`tag:${tag}`, key)
159
+ }
160
+ }
161
+
162
+ async function invalidateByTag(tag: string) {
163
+ const keys = await client.sMembers(`tag:${tag}`)
164
+ if (keys.length > 0) {
165
+ await client.del(...keys)
166
+ }
167
+ await client.del(`tag:${tag}`)
168
+ }
169
+ ```
170
+
171
+ ### Rate Limiting
172
+ ```typescript
173
+ async function checkRateLimit(userId: string, limit: number, window: number): Promise<boolean> {
174
+ const key = `ratelimit:${userId}`
175
+ const current = await client.incr(key)
176
+
177
+ if (current === 1) {
178
+ await client.expire(key, window)
179
+ }
180
+
181
+ return current <= limit
182
+ }
183
+ ```
184
+
185
+ ### Session Storage
186
+ ```typescript
187
+ async function setSession(sessionId: string, data: any, ttl: number) {
188
+ await client.set(`session:${sessionId}`, JSON.stringify(data), { EX: ttl })
189
+ }
190
+
191
+ async function getSession(sessionId: string) {
192
+ const data = await client.get(`session:${sessionId}`)
193
+ return data ? JSON.parse(data) : null
194
+ }
195
+
196
+ async function deleteSession(sessionId: string) {
197
+ await client.del(`session:${sessionId}`)
198
+ }
199
+ ```
200
+
201
+ ### Distributed Locks
202
+ ```typescript
203
+ async function acquireLock(key: string, ttl: number): Promise<boolean> {
204
+ const result = await client.set(key, 'locked', {
205
+ EX: ttl,
206
+ NX: true, // Only set if not exists
207
+ })
208
+ return result === 'OK'
209
+ }
210
+
211
+ async function releaseLock(key: string) {
212
+ await client.del(key)
213
+ }
214
+ ```
215
+
216
+ ## Best Practices
217
+
218
+ ✅ **DO:**
219
+ - Use connection pooling
220
+ - Set appropriate TTL for cached data
221
+ - Use pipelining for multiple operations
222
+ - Use transactions (MULTI/EXEC) for atomic operations
223
+ - Monitor memory usage
224
+ - Use appropriate data structures (hash for objects, set for unique values)
225
+ - Implement cache invalidation strategies
226
+ - Use Redis Cluster for high availability
227
+ - Enable persistence (RDB or AOF) for production
228
+ - Use Redis Sentinel for failover
229
+
230
+ ❌ **DON'T:**
231
+ - Store large values (> 100KB, use compression or external storage)
232
+ - Use Redis as primary database (it's a cache)
233
+ - Skip error handling
234
+ - Ignore memory limits
235
+ - Hardcode connection strings
236
+ - Use KEYS command in production (use SCAN instead)
237
+ - Store sensitive data without encryption
238
+ - Skip connection retry logic
239
+ - Ignore eviction policies
240
+ - Use blocking operations without timeouts
241
+
242
+ ## Configuration
243
+
244
+ ### Environment Variables
245
+ ```bash
246
+ REDIS_URL=redis://localhost:6379
247
+ REDIS_URL=redis://:password@host:6379
248
+ REDIS_URL=rediss://host:6380 # SSL
249
+ REDIS_HOST=localhost
250
+ REDIS_PORT=6379
251
+ REDIS_PASSWORD=securepassword
252
+ ```
253
+
254
+ ### Docker Compose
255
+ ```yaml
256
+ services:
257
+ redis:
258
+ image: redis:7-alpine
259
+ ports:
260
+ - "6379:6379"
261
+ command: redis-server --requirepass securepassword --appendonly yes
262
+ volumes:
263
+ - redis_data:/data
264
+ healthcheck:
265
+ test: ["CMD", "redis-cli", "ping"]
266
+ interval: 10s
267
+ timeout: 5s
268
+ retries: 5
269
+
270
+ volumes:
271
+ redis_data:
272
+ ```
273
+
274
+ ## Integration with Development
275
+
276
+ ### Testing
277
+ ```typescript
278
+ // Use test Redis instance
279
+ const testClient = createClient({
280
+ url: 'redis://localhost:6380', // Different port for tests
281
+ })
282
+
283
+ // Clean up after tests
284
+ afterEach(async () => {
285
+ await testClient.flushDb()
286
+ })
287
+ ```
288
+
289
+ ### Health Checks
290
+ ```typescript
291
+ async function checkRedisHealth(): Promise<boolean> {
292
+ try {
293
+ await client.ping()
294
+ return true
295
+ } catch {
296
+ return false
297
+ }
298
+ }
299
+ ```
300
+
301
+ <!-- REDIS:END -->
302
+
@@ -0,0 +1,308 @@
1
+ ---
2
+ name: "S3"
3
+ description: "Use AWS S3 for object storage, file uploads, static assets, and backup storage with high availability."
4
+ version: "1.0.0"
5
+ category: "services"
6
+ author: "Rulebook"
7
+ tags: ["services", "storage"]
8
+ dependencies: []
9
+ conflicts: []
10
+ ---
11
+ <!-- S3:START -->
12
+ # AWS S3 Storage Instructions
13
+
14
+ **CRITICAL**: Use AWS S3 for object storage, file uploads, static assets, and backup storage with high availability.
15
+
16
+ ## Core Features
17
+
18
+ ### Connection
19
+ ```typescript
20
+ // Using @aws-sdk/client-s3
21
+ import { S3Client, PutObjectCommand, GetObjectCommand, DeleteObjectCommand } from '@aws-sdk/client-s3'
22
+
23
+ const s3Client = new S3Client({
24
+ region: process.env.AWS_REGION || 'us-east-1',
25
+ credentials: {
26
+ accessKeyId: process.env.AWS_ACCESS_KEY_ID || '',
27
+ secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || '',
28
+ },
29
+ })
30
+
31
+ // Using AWS SDK v2
32
+ import AWS from 'aws-sdk'
33
+
34
+ const s3 = new AWS.S3({
35
+ accessKeyId: process.env.AWS_ACCESS_KEY_ID,
36
+ secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
37
+ region: process.env.AWS_REGION || 'us-east-1',
38
+ })
39
+ ```
40
+
41
+ ### Basic Operations
42
+ ```typescript
43
+ // Upload file
44
+ const uploadParams = {
45
+ Bucket: process.env.S3_BUCKET || 'my-bucket',
46
+ Key: 'path/to/file.jpg',
47
+ Body: fileBuffer,
48
+ ContentType: 'image/jpeg',
49
+ ACL: 'private', // or 'public-read'
50
+ }
51
+
52
+ await s3Client.send(new PutObjectCommand(uploadParams))
53
+
54
+ // Upload with metadata
55
+ await s3Client.send(new PutObjectCommand({
56
+ ...uploadParams,
57
+ Metadata: {
58
+ userId: '123',
59
+ originalName: 'photo.jpg',
60
+ },
61
+ TagSet: [
62
+ { Key: 'category', Value: 'profile' },
63
+ ],
64
+ }))
65
+
66
+ // Get file
67
+ const getParams = {
68
+ Bucket: process.env.S3_BUCKET,
69
+ Key: 'path/to/file.jpg',
70
+ }
71
+
72
+ const response = await s3Client.send(new GetObjectCommand(getParams))
73
+ const fileContent = await response.Body?.transformToByteArray()
74
+
75
+ // Delete file
76
+ await s3Client.send(new DeleteObjectCommand({
77
+ Bucket: process.env.S3_BUCKET,
78
+ Key: 'path/to/file.jpg',
79
+ }))
80
+ ```
81
+
82
+ ### Advanced Features
83
+ ```typescript
84
+ // Presigned URL for upload
85
+ import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
86
+
87
+ const command = new PutObjectCommand({
88
+ Bucket: process.env.S3_BUCKET,
89
+ Key: 'uploads/file.jpg',
90
+ ContentType: 'image/jpeg',
91
+ })
92
+
93
+ const presignedUrl = await getSignedUrl(s3Client, command, { expiresIn: 3600 })
94
+
95
+ // Presigned URL for download
96
+ const getCommand = new GetObjectCommand({
97
+ Bucket: process.env.S3_BUCKET,
98
+ Key: 'path/to/file.jpg',
99
+ })
100
+
101
+ const downloadUrl = await getSignedUrl(s3Client, getCommand, { expiresIn: 3600 })
102
+
103
+ // Multipart upload (for large files)
104
+ import { CreateMultipartUploadCommand, UploadPartCommand, CompleteMultipartUploadCommand } from '@aws-sdk/client-s3'
105
+
106
+ const createCommand = new CreateMultipartUploadCommand({
107
+ Bucket: process.env.S3_BUCKET,
108
+ Key: 'large-file.zip',
109
+ ContentType: 'application/zip',
110
+ })
111
+
112
+ const { UploadId } = await s3Client.send(createCommand)
113
+
114
+ // Upload parts
115
+ const part1 = await s3Client.send(new UploadPartCommand({
116
+ Bucket: process.env.S3_BUCKET,
117
+ Key: 'large-file.zip',
118
+ PartNumber: 1,
119
+ UploadId,
120
+ Body: part1Buffer,
121
+ }))
122
+
123
+ // Complete upload
124
+ await s3Client.send(new CompleteMultipartUploadCommand({
125
+ Bucket: process.env.S3_BUCKET,
126
+ Key: 'large-file.zip',
127
+ UploadId,
128
+ MultipartUpload: {
129
+ Parts: [
130
+ { PartNumber: 1, ETag: part1.ETag },
131
+ ],
132
+ },
133
+ }))
134
+
135
+ // List objects
136
+ import { ListObjectsV2Command } from '@aws-sdk/client-s3'
137
+
138
+ const listCommand = new ListObjectsV2Command({
139
+ Bucket: process.env.S3_BUCKET,
140
+ Prefix: 'uploads/',
141
+ MaxKeys: 100,
142
+ })
143
+
144
+ const { Contents } = await s3Client.send(listCommand)
145
+ ```
146
+
147
+ ## Common Patterns
148
+
149
+ ### File Upload Handler
150
+ ```typescript
151
+ async function uploadFile(file: Buffer, filename: string, userId: string) {
152
+ const key = `users/${userId}/${Date.now()}-${filename}`
153
+
154
+ await s3Client.send(new PutObjectCommand({
155
+ Bucket: process.env.S3_BUCKET,
156
+ Key: key,
157
+ Body: file,
158
+ ContentType: getContentType(filename),
159
+ Metadata: {
160
+ userId,
161
+ originalName: filename,
162
+ uploadedAt: new Date().toISOString(),
163
+ },
164
+ }))
165
+
166
+ return {
167
+ key,
168
+ url: `https://${process.env.S3_BUCKET}.s3.${process.env.AWS_REGION}.amazonaws.com/${key}`,
169
+ }
170
+ }
171
+ ```
172
+
173
+ ### Temporary File Access
174
+ ```typescript
175
+ async function generateTemporaryDownloadUrl(key: string, expiresIn: number = 3600) {
176
+ const command = new GetObjectCommand({
177
+ Bucket: process.env.S3_BUCKET,
178
+ Key: key,
179
+ })
180
+
181
+ return await getSignedUrl(s3Client, command, { expiresIn })
182
+ }
183
+ ```
184
+
185
+ ### File Cleanup
186
+ ```typescript
187
+ async function deleteFilesByPrefix(prefix: string) {
188
+ const listCommand = new ListObjectsV2Command({
189
+ Bucket: process.env.S3_BUCKET,
190
+ Prefix: prefix,
191
+ })
192
+
193
+ let continuationToken: string | undefined
194
+
195
+ do {
196
+ const response = await s3Client.send({
197
+ ...listCommand,
198
+ ContinuationToken: continuationToken,
199
+ })
200
+
201
+ if (response.Contents) {
202
+ for (const object of response.Contents) {
203
+ if (object.Key) {
204
+ await s3Client.send(new DeleteObjectCommand({
205
+ Bucket: process.env.S3_BUCKET,
206
+ Key: object.Key,
207
+ }))
208
+ }
209
+ }
210
+ }
211
+
212
+ continuationToken = response.NextContinuationToken
213
+ } while (continuationToken)
214
+ }
215
+ ```
216
+
217
+ ## Best Practices
218
+
219
+ ✅ **DO:**
220
+ - Use presigned URLs for client uploads
221
+ - Set appropriate Content-Type headers
222
+ - Use versioning for important files
223
+ - Implement lifecycle policies for cleanup
224
+ - Use multipart upload for files > 5MB
225
+ - Enable encryption (SSE-S3 or SSE-KMS)
226
+ - Use appropriate storage classes (Standard, IA, Glacier)
227
+ - Implement proper error handling
228
+ - Use bucket policies for access control
229
+ - Monitor bucket usage and costs
230
+
231
+ ❌ **DON'T:**
232
+ - Store sensitive data without encryption
233
+ - Use public-read ACL unnecessarily
234
+ - Hardcode credentials
235
+ - Ignore error handling
236
+ - Skip content-type validation
237
+ - Store large files without multipart upload
238
+ - Ignore lifecycle policies
239
+ - Skip access logging
240
+ - Use default bucket policies
241
+ - Ignore cost optimization
242
+
243
+ ## Configuration
244
+
245
+ ### Environment Variables
246
+ ```bash
247
+ AWS_REGION=us-east-1
248
+ AWS_ACCESS_KEY_ID=your-access-key
249
+ AWS_SECRET_ACCESS_KEY=your-secret-key
250
+ S3_BUCKET=my-bucket
251
+ ```
252
+
253
+ ### IAM Policy
254
+ ```json
255
+ {
256
+ "Version": "2012-10-17",
257
+ "Statement": [
258
+ {
259
+ "Effect": "Allow",
260
+ "Action": [
261
+ "s3:PutObject",
262
+ "s3:GetObject",
263
+ "s3:DeleteObject"
264
+ ],
265
+ "Resource": "arn:aws:s3:::my-bucket/*"
266
+ }
267
+ ]
268
+ }
269
+ ```
270
+
271
+ ## Integration with Development
272
+
273
+ ### Testing
274
+ ```typescript
275
+ // Use test bucket or LocalStack
276
+ const testS3Client = new S3Client({
277
+ endpoint: 'http://localhost:4566', // LocalStack
278
+ region: 'us-east-1',
279
+ credentials: {
280
+ accessKeyId: 'test',
281
+ secretAccessKey: 'test',
282
+ },
283
+ forcePathStyle: true,
284
+ })
285
+
286
+ // Clean up after tests
287
+ afterEach(async () => {
288
+ // Delete test files
289
+ })
290
+ ```
291
+
292
+ ### Health Checks
293
+ ```typescript
294
+ async function checkS3Health(): Promise<boolean> {
295
+ try {
296
+ await s3Client.send(new ListObjectsV2Command({
297
+ Bucket: process.env.S3_BUCKET,
298
+ MaxKeys: 1,
299
+ }))
300
+ return true
301
+ } catch {
302
+ return false
303
+ }
304
+ }
305
+ ```
306
+
307
+ <!-- S3:END -->
308
+