|
| 1 | +# GitHub Copilot Instructions for lambda-api |
| 2 | + |
| 3 | +This repository contains **lambda-api**, a lightweight web framework for AWS Lambda serverless applications. This framework has **ZERO dependencies** and is designed specifically for serverless applications using AWS Lambda and API Gateway. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +Lambda API is a web framework that closely mirrors Express.js and Fastify but is significantly stripped down to maximize performance with Lambda's stateless, single-run executions. It provides routing, HTML serving, redirects, binary file serving, middleware, error handling, and more. |
| 8 | + |
| 9 | +## Architecture & Code Structure |
| 10 | + |
| 11 | +- **`index.js`** - Main API class and entry point |
| 12 | +- **`lib/`** - Core library modules: |
| 13 | + - `request.js` - Request handling and parsing |
| 14 | + - `response.js` - Response formatting and methods |
| 15 | + - `utils.js` - Utility functions |
| 16 | + - `logger.js` - Built-in logging engine |
| 17 | + - `errors.js` - Custom error classes |
| 18 | + - `compression.js` - Response compression handling |
| 19 | + - `s3-service.js` - S3 integration for file operations |
| 20 | + - `statusCodes.js` - HTTP status code mappings |
| 21 | + - `mimemap.js` - MIME type mappings |
| 22 | + - `prettyPrint.js` - Route debugging and visualization |
| 23 | +- **`__tests__/`** - Jest unit tests for all functionality |
| 24 | +- **`index.d.ts`** - TypeScript type definitions |
| 25 | + |
| 26 | +## Development Guidelines |
| 27 | + |
| 28 | +### Code Style & Conventions |
| 29 | + |
| 30 | +- **Style**: Use JavaScript ES6+ features, strict mode (`'use strict'`) |
| 31 | +- **Quotes**: Single quotes for strings (enforced by Prettier) |
| 32 | +- **Code quality**: ESLint with `eslint:recommended` and Prettier integration |
| 33 | +- **Comments**: Use JSDoc style for file headers with author and license |
| 34 | +- **Error handling**: Use custom error classes from `lib/errors.js` (ConfigurationError, ApiError, FileError) |
| 35 | +- **Async patterns**: Support both async/await and Promises |
| 36 | +- **No external dependencies**: This is a core principle - do not add external npm packages |
| 37 | + |
| 38 | +### Testing |
| 39 | + |
| 40 | +- **Framework**: Jest |
| 41 | +- **Test files**: `__tests__/*.unit.js` |
| 42 | +- **Test structure**: Each module has corresponding unit tests |
| 43 | +- **Coverage**: Aim for high test coverage |
| 44 | +- **Run tests**: `npm test` (runs both type tests and unit tests) |
| 45 | + - Type tests: `npm run test:types` |
| 46 | + - Unit tests only: `npm run test:unit` |
| 47 | + - With coverage: `npm run test-cov` |
| 48 | + |
| 49 | +### Build & Development Commands |
| 50 | + |
| 51 | +```bash |
| 52 | +# Install dependencies |
| 53 | +npm ci |
| 54 | + |
| 55 | +# Run all tests (TypeScript types + Jest unit tests) |
| 56 | +npm test |
| 57 | + |
| 58 | +# Linting |
| 59 | +npm run lint:check # Check for lint errors |
| 60 | +npm run lint:fix # Auto-fix lint errors |
| 61 | + |
| 62 | +# Code formatting |
| 63 | +npm run prettier:check # Check formatting |
| 64 | +npm run prettier:write # Auto-format code |
| 65 | + |
| 66 | +# CI test command (includes linting, formatting, tests, and coverage) |
| 67 | +npm run test-ci |
| 68 | +``` |
| 69 | + |
| 70 | +### Key Features & Patterns |
| 71 | + |
| 72 | +1. **Route definition**: Similar to Express.js |
| 73 | + |
| 74 | + ```javascript |
| 75 | + api.get('/path', async (req, res) => { |
| 76 | + return { status: 'ok' }; |
| 77 | + }); |
| 78 | + ``` |
| 79 | + |
| 80 | +2. **Middleware support**: Method-based and global middleware |
| 81 | +3. **Error handling**: Built-in error middleware system |
| 82 | +4. **Binary support**: Automatic base64 encoding/decoding |
| 83 | +5. **Compression**: Built-in gzip/brotli compression |
| 84 | +6. **S3 integration**: Pre-signed URL generation via `getLink()` |
| 85 | +7. **Logger**: Configurable logging with sampling support |
| 86 | +8. **Multi-version support**: API Gateway v1/v2 and ALB payload formats |
| 87 | + |
| 88 | +### AWS Integration |
| 89 | + |
| 90 | +- Designed for AWS Lambda Proxy Integration and ALB Lambda Target Support |
| 91 | +- Parses API Gateway and ALB event formats (v1 and v2) |
| 92 | +- Automatically handles request parsing and response formatting |
| 93 | +- Supports binary file uploads/downloads via base64 encoding |
| 94 | + |
| 95 | +### TypeScript Support |
| 96 | + |
| 97 | +- Type definitions in `index.d.ts` |
| 98 | +- Type tests in `index.test-d.ts` using `tsd` |
| 99 | +- Ensure changes maintain TypeScript compatibility |
| 100 | + |
| 101 | +### Making Changes |
| 102 | + |
| 103 | +1. **Minimal dependencies**: Never add external npm dependencies without strong justification |
| 104 | +2. **Test coverage**: Add unit tests for new features in `__tests__/` |
| 105 | +3. **Type definitions**: Update `index.d.ts` for public API changes |
| 106 | +4. **Documentation**: Update README.md for user-facing changes |
| 107 | +5. **Backwards compatibility**: Avoid breaking changes when possible |
| 108 | +6. **Performance**: Keep the framework lightweight and fast |
| 109 | +7. **Serverless-first**: Design with Lambda's stateless execution model in mind |
| 110 | + |
| 111 | +### Security Considerations |
| 112 | + |
| 113 | +- Validate all user inputs |
| 114 | +- Sanitize HTML output (use `escapeHtml` from `lib/utils.js`) |
| 115 | +- Be cautious with file operations and S3 integrations |
| 116 | +- Ensure proper error handling to avoid information leakage |
| 117 | +- Use secure defaults for cookies and headers |
| 118 | + |
| 119 | +### Common Patterns |
| 120 | + |
| 121 | +- **Route handlers**: Accept `(req, res, next)` parameters |
| 122 | +- **Async responses**: Can return values directly or use `res.send()`, `res.json()`, etc. |
| 123 | +- **Error propagation**: Use `res.error()` or throw errors to trigger error middleware |
| 124 | +- **Path parameters**: Extracted automatically (e.g., `/user/:id`) |
| 125 | +- **Middleware chaining**: Use `next()` to continue to next middleware/handler |
| 126 | + |
| 127 | +### CI/CD |
| 128 | + |
| 129 | +- GitHub Actions workflows in `.github/workflows/` |
| 130 | +- Automated testing on push to main branch |
| 131 | +- Coverage reports sent to Coveralls |
| 132 | +- Requires Node.js 14+ (currently testing on 14.x) |
| 133 | + |
| 134 | +## Notes for Copilot |
| 135 | + |
| 136 | +- This is a production library used by many serverless applications |
| 137 | +- Prioritize backwards compatibility and stability |
| 138 | +- Keep the codebase simple and maintainable |
| 139 | +- Performance is critical - avoid adding overhead |
| 140 | +- The zero-dependency philosophy is non-negotiable |
| 141 | +- When suggesting changes, ensure they align with Express.js-like API patterns |
| 142 | +- Test thoroughly with both API Gateway v1/v2 and ALB event formats |
0 commit comments