Skip to content

feat: raw email export, body formats, and code quality refactors#3

Merged
marcfargas merged 4 commits intomainfrom
develop
Feb 27, 2026
Merged

feat: raw email export, body formats, and code quality refactors#3
marcfargas merged 4 commits intomainfrom
develop

Conversation

@marcfargas
Copy link
Owner

@marcfargas marcfargas commented Feb 27, 2026

Summary

Three feature commits + one refactor commit + changesets for 0.4.0.

feat: --format=eml / --format=mbox (raw RFC 2822 export)

  • go-gmail get <id> --format=eml — full RFC 2822 message bytes
  • go-gmail thread <id> --format=mbox — full thread as mbox
  • --output=<path> / --b64encode output modes
  • Fixes message/rfc822 embedded attachment retrieval (bypasses broken attachments.get)

feat: --format=text|html|sane-html for get

  • Plain text and raw HTML body extraction
  • sane-html: sanitized via sanitize-html (strips scripts, events, javascript: URIs)

refactor: extract shared helpers, fix test copies, remove dead code

  • Extract parseFlags/readBodyFlagssrc/bin/gmail-flags.ts
  • Extract gmailApi/handleApiErrorsrc/gmail/api.ts
  • Extract serializeMimePart() MIME helper
  • Remove duplicate test coverage, dead import, fix return type

chore: changesets for 0.4.0

Three changeset files — changesets/action will bump to 0.4.0 on merge.

Tests

320/322 passing (−2 intentionally deleted duplicate tests)

New CLI flags on get and thread commands:
  go-gmail <account> get <id> --format=eml [--output=<path>] [--b64encode]
  go-gmail <account> thread <id> --format=mbox [--output=<path>] [--b64encode]

Output modes:
  --output=<path>  write bytes to file → JSON { ok, path, bytes }
  --b64encode      JSON { format, data, bytes } — agent-safe
  (neither)        raw bytes to stdout — pipe with > file.eml

Implementation:
  src/gmail/raw.ts — getMessageRaw() and getThreadMbox()
  - getMessageRaw: messages.get(format=raw) → Buffer
  - getThreadMbox: parallel raw fetch per message + mbox assembly
    (envelope From lines, CRLF→LF, mboxo escaping)

Also fixes the message/rfc822 attachment bug: --format=eml returns the
complete outer message including embedded .eml attachments, bypassing
the broken attachments.get endpoint.

10 new tests (297/297 total passing)
New output formats for 'go-gmail <account> get <messageId>':
  --format=text       plain text body (body.text)
  --format=html       raw HTML body (body.html)
  --format=sane-html  sanitized HTML (safe to render)

sane-html strips: <script>, <iframe>, <object>, <form>, all event
handlers (onclick/onerror/onload/…), javascript: hrefs, data: image
URIs. Keeps: tables, style attributes, cid: images, all layout tags.
Adds rel=noopener noreferrer to target=_blank links.

All formats support --output=<path> and --b64encode, matching the
existing eml/mbox output contract.

New: src/gmail/formats.ts — sanitizeEmailHtml() using sanitize-html
New dep: sanitize-html (+ @types/sanitize-html)
25 new tests (322/322 total passing)
… code

- Extract parseFlags/readBodyFlags to src/bin/gmail-flags.ts (tests now import production code instead of local copies)

- Extract gmailApi/handleApiError to src/gmail/api.ts (eliminates byte-for-byte duplication between index.ts and raw.ts)

- Extract serializeMimePart() helper, used by both buildMimeMessage and buildForwardMime (eliminates 6-line copy-paste)

- Remove duplicate error-handling describe block in index.test.ts

- Remove dead 'lookup' import from helpers.ts

- Fix handleRawOutput return type: unknown|undefined → object|undefined
@marcfargas marcfargas closed this Feb 27, 2026
@marcfargas marcfargas reopened this Feb 27, 2026
@marcfargas marcfargas merged commit 5a516d5 into main Feb 27, 2026
6 checks passed
@github-actions github-actions bot mentioned this pull request Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant