{"id":42013584,"url":"https://github.com/git-stunts/trailer-codec","last_synced_at":"2026-01-26T02:45:37.508Z","repository":{"id":332072493,"uuid":"1130076890","full_name":"git-stunts/trailer-codec","owner":"git-stunts","description":"A robust, secure utility for manipulating structured metadata in Git commit messages.","archived":false,"fork":false,"pushed_at":"2026-01-12T18:15:00.000Z","size":180,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-01-12T18:36:05.459Z","etag":null,"topics":["commit-messages","git","git-plumbing","git-stunts","git-trailers"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/git-stunts.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":"NOTICE","maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-01-08T02:05:11.000Z","updated_at":"2026-01-12T09:22:23.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/git-stunts/trailer-codec","commit_stats":null,"previous_names":["git-stunts/trailer-codec"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/git-stunts/trailer-codec","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/git-stunts%2Ftrailer-codec","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/git-stunts%2Ftrailer-codec/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/git-stunts%2Ftrailer-codec/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/git-stunts%2Ftrailer-codec/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/git-stunts","download_url":"https://codeload.github.com/git-stunts/trailer-codec/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/git-stunts%2Ftrailer-codec/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28765351,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-26T02:25:41.078Z","status":"ssl_error","status_checked_at":"2026-01-26T02:24:28.809Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["commit-messages","git","git-plumbing","git-stunts","git-trailers"],"created_at":"2026-01-26T02:45:36.207Z","updated_at":"2026-01-26T02:45:37.496Z","avatar_url":"https://github.com/git-stunts.png","language":"JavaScript","readme":"# @git-stunts/trailer-codec\n\n[![npm version](https://img.shields.io/npm/v/@git-stunts/trailer-codec.svg)](https://www.npmjs.com/package/@git-stunts/trailer-codec)\n[![CI](https://github.com/git-stunts/trailer-codec/actions/workflows/ci.yml/badge.svg)](https://github.com/git-stunts/trailer-codec/actions/workflows/ci.yml)\n[![license](https://img.shields.io/npm/l/@git-stunts/trailer-codec.svg)](LICENSE)\n\n\u003cimg width=\"420\" src=\"https://github.com/user-attachments/assets/0a3800d9-12c4-4639-b6c3-b1782bf28c96\" align=\"right\" /\u003e\n\nA robust encoder/decoder for structured metadata within Git commit messages.\n\n### Key Features\n\n- **Standard Compliant**: Follows the Git \"trailer\" convention (RFC 822 / Email headers)\n- **DoS Protection**: Built-in 5MB message size limit to prevent attacks\n- **Structured Domain**: Formalized entities and value objects for type safety\n- **Zod Validation**: Schema-driven validation with helpful error messages\n- **Case Normalization**: Trailer keys normalized to lowercase for consistency\n- **Pure Domain Logic**: No I/O, no Git subprocess execution\n\n### Design Principles\n\n1. **Domain Purity**: Core logic independent of infrastructure\n2. **Type Safety**: Value Objects ensure data validity at instantiation\n3. **Immutability**: All entities are immutable\n4. **Separation of Concerns**: Encoding/decoding in dedicated service\n\n## Prerequisites\n\n- **Node.js**: \u003e= 20.0.0\n\n## Installation\n\n```bash\nnpm install @git-stunts/trailer-codec\n```\n\n## Developer \u0026 Testing\n\n- **Node.js ≥ 20** matches the `engines` field in `package.json` and is required for Vitest/ESM support.\n- `npm test` runs the Vitest suite, `npm run lint` validates the code with ESLint, and `npm run format` formats files with Prettier; all scripts target the entire repo root.\n- Consult `TESTING.md` for run modes, test filters, and tips for extending the suite before submitting contributions.\n\n## Usage\n\n### Basic Encoding/Decoding\n\n```javascript\nimport { createDefaultTrailerCodec } from '@git-stunts/trailer-codec';\n\nconst codec = createDefaultTrailerCodec();\nconst message = codec.encode({\n  title: 'feat: add user authentication',\n  body: 'Implemented OAuth2 flow with JWT tokens.',\n  trailers: [\n    { key: 'Signed-off-by', value: 'James Ross' },\n    { key: 'Reviewed-by', value: 'Alice Smith' },\n  ],\n});\n\nconsole.log(message);\n// feat: add user authentication\n//\n// Implemented OAuth2 flow with JWT tokens.\n//\n// signed-off-by: James Ross\n// reviewed-by: Alice Smith\n\nconst decoded = codec.decode(message);\nconsole.log(decoded.title);      // \"feat: add user authentication\"\nconsole.log(decoded.trailers);   // { 'signed-off-by': 'James Ross', 'reviewed-by': 'Alice Smith' }\n```\n\n### API Patterns\n\n- **Primary entry point**: `createDefaultTrailerCodec()` returns a `TrailerCodec` wired with a fresh `TrailerCodecService`; use `.encode()`/`.decode()` (or `.encodeMessage()`/`.decodeMessage()`) to keep configuration in one place.\n- **Facade**: `TrailerCodec` keeps configuration near instantiation while still leveraging `createMessageHelpers()` under the hood (pass your own service when you need control).\n- **Advanced**: `createConfiguredCodec()` and direct `TrailerCodecService` usage let you swap schema bundles, parsers, formatters, or helper overrides when you need custom validation or formatting behavior. The standalone helpers `encodeMessage()`/`decodeMessage()` remain available as deprecated convenience wrappers.\n\n### Breaking Changes\n\n- `decodeMessage()` now trims trailing newlines in the version `v0.2.0+` runtime, so plain string inputs will no longer include a final `\\n` unless you opt into it.\n- To preserve the trailing newline you rely on (e.g., when round-tripping commit templates), either instantiate `TrailerCodec` with `bodyFormatOptions: { keepTrailingNewline: true }`, call `formatBodySegment(body, { keepTrailingNewline: true })` yourself, or pass the same option through `createConfiguredCodec`.\n- See [`docs/MIGRATION.md#v020`](docs/MIGRATION.md#v020) for the full migration checklist and decoding behavior rationale.\n\n### Body Formatting \u0026 Facade\n\n`decodeMessage` now trims the decoded body by default, returning the content exactly as stored; no extra newline is appended automatically. If you still need the trailing newline (for example when writing the decoded body back into a commit template), instantiate the helpers or facade with `bodyFormatOptions: { keepTrailingNewline: true }`:\n\n```javascript\nimport TrailerCodec from '@git-stunts/trailer-codec';\n\nconst codec = new TrailerCodec({ bodyFormatOptions: { keepTrailingNewline: true } });\nconst payload = codec.decode('Title\\n\\nBody\\n');\nconsole.log(payload.body); // 'Body\\n'\n```\n\nYou can also call the exported `formatBodySegment(body, { keepTrailingNewline: true })` helper directly when you need the formatting logic elsewhere.\n\n```javascript\nimport { formatBodySegment } from '@git-stunts/trailer-codec';\n\nconst trimmed = formatBodySegment('Body\\n', { keepTrailingNewline: true });\nconsole.log(trimmed); // 'Body\\n'\n```\n\n### Advanced\n\n#### Configured Codec Builder\n\nWhen you need a prewired codec (custom key patterns, parser tweaks, formatter hooks), use `createConfiguredCodec({ keyPattern, keyMaxLength, parserOptions })`. It builds a schema bundle, parser, and service for you, and returns helpers so you can immediately call `decodeMessage`/`encodeMessage`:\n\n```javascript\nimport { createConfiguredCodec } from '@git-stunts/trailer-codec';\n\nconst { decodeMessage, encodeMessage } = createConfiguredCodec({\n  keyPattern: '[A-Za-z._-]+',\n  keyMaxLength: 120,\n  parserOptions: {},\n});\n\nconst payload = { title: 'feat: cli docs', trailers: { 'Custom.Key': 'value' } };\nconst encoded = encodeMessage(payload);\nconst decoded = decodeMessage(encoded);\nconsole.log(decoded.title); // 'feat: cli docs'\n```\n\n#### Domain Entities\n\n```javascript\nimport { GitCommitMessage } from '@git-stunts/trailer-codec';\n\nconst msg = new GitCommitMessage({\n  title: 'fix: resolve memory leak',\n  body: 'Fixed WeakMap reference cycle.',\n  trailers: [\n    { key: 'Issue', value: 'GH-123' },\n    { key: 'Signed-off-by', value: 'James Ross' }\n  ]\n});\n\nconsole.log(msg.toString());\n```\n\n#### Public API Helpers \u0026 Configuration\n\n- `formatBodySegment(body, { keepTrailingNewline = false })` mirrors the helper powering `decodeMessage`, trimming whitespace while optionally preserving the trailing newline when you plan to write the body back into a template.\n- `createMessageHelpers({ service, bodyFormatOptions })` returns `{ decodeMessage, encodeMessage }` bound to the provided `TrailerCodecService`; pass `bodyFormatOptions` to control whether decoded bodies keep their trailing newline.\n- `TrailerCodec` wraps `createMessageHelpers()` so you can instantiate a codec class with custom `service` or `bodyFormatOptions` and still leverage the helper contract via `encode()`/`decode()`.\n- `createConfiguredCodec({ keyPattern, keyMaxLength, parserOptions, formatters, bodyFormatOptions })` wires together `createGitTrailerSchemaBundle`, `TrailerParser`, `TrailerCodecService`, and the helper pair, letting you configure key validation, parser heuristics, formatting hooks, and body formatting in a single call.\n- `TrailerCodecService` exposes the schema bundle, parser, trailer factory, formatter hooks, and helper utilities (`MessageNormalizer`, `extractTitle`, `composeBody`); see `docs/SERVICE.md` for a deeper explanation of how to customize each stage without touching the core service.\n\n## ✅ Validation Rules\n\nTrailer codec enforces strict validation via the concrete subclasses of `TrailerCodecError`:\n\n| Rule | Constraint | Thrown Error |\n|------|------------|--------------|\n| **Message Size** | ≤ 5MB | `TrailerTooLargeError` |\n| **Title** | Must be a non-empty string | `CommitMessageInvalidError` (during entity construction) |\n| **Trailer Key** | Alphanumeric, hyphens, underscores only (`/^[A-Za-z0-9_-]+$/`) and ≤ 100 characters (prevents ReDoS) | `TrailerInvalidError` |\n| **Trailer Value** | Cannot contain carriage returns or line feeds and must not be empty | `TrailerValueInvalidError` |\n\n**Key Normalization:** All trailer keys are automatically normalized to lowercase (e.g., `Signed-Off-By` → `signed-off-by`).\n\n**Blank-Line Guard:** Trailers must be separated from the body by a blank line; omitting the separator throws `TrailerNoSeparatorError`.\n\n### Validation Errors\n\nWhen `TrailerCodecService` or the exported helpers throw, they surface one of the following classes so you can recover with `instanceof` checks:\n\n| Error | Trigger | Suggested Fix |\n| --- | --- | --- |\n| `TrailerTooLargeError` | Message exceeds 5MB while `MessageNormalizer.guardMessageSize()` runs | Split the commit or remove content until the payload fits. |\n| `TrailerNoSeparatorError` | Missing blank line before trailers when `TrailerParser.split()` runs | Insert the required empty line between body and trailers. |\n| `TrailerValueInvalidError` | Trailer value includes newline characters or fails the schema value rules | Remove or escape newline characters before encoding. |\n| `TrailerInvalidError` | Trailer key/value pair fails the schema validation (`GitTrailerSchema`) | Adjust the key/value or supply a custom schema bundle via `TrailerCodecService`. |\n| `CommitMessageInvalidError` | `GitCommitMessageSchema` rejects the full payload (title/body/trailers) | Fix the invalid field or pass a conforming payload; use formatters if needed. |\n\nAll of the above inherit from `TrailerCodecError` (`src/domain/errors/TrailerCodecError.js`) and expose `meta` for diagnostics; prefer checking the specific class instead of inspecting `code`.\n\n## 🛡️ Security\n\n- **No Code Execution**: Pure string manipulation, no `eval()` or dynamic execution\n- **DoS Protection**: Rejects messages \u003e 5MB\n- **ReDoS Prevention**: Max key length limits regex execution time\n- **No Git Subprocess**: Library performs no I/O operations\n- **Line Injection Guard**: Trailer values omit newline characters so no unexpected trailers can be injected\n\nSee [SECURITY.md](SECURITY.md) for details.\n\n## 📚 Additional Documentation\n\n- [`docs/ADVANCED.md`](docs/ADVANCED.md) — Custom schema injection, validation overrides, and advanced integration patterns.\n- [`docs/PARSER.md`](docs/PARSER.md) — Step-by-step explanation of the backward-walk parser.\n- [`docs/INTEGRATION.md`](docs/INTEGRATION.md) — Git log scripting, streaming decoder, and Git-CMS filtering recipes.\n- [`docs/SERVICE.md`](docs/SERVICE.md) — How `TrailerCodecService` wires schema, parser, and formatter helpers for customization.\n- [`API_REFERENCE.md`](API_REFERENCE.md) — Complete catalog of the public exports, their inputs/outputs, and notable knobs.\n- [`TESTING.md`](TESTING.md) — How to run/extend the Vitest, lint, and format scripts plus contributor tips.\n- **Git hooks**: Run `npm run setuphooks` once per clone to point `core.hooksPath` at `scripts/`. The hook now runs just `npm run lint` and `npm run format` before each commit.\n\n## License\n\nApache-2.0\nCopyright © 2026 [James Ross](https://github.com/flyingrobots)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgit-stunts%2Ftrailer-codec","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgit-stunts%2Ftrailer-codec","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgit-stunts%2Ftrailer-codec/lists"}