mailfile 0.1.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 (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +203 -0
  3. package/dist/mailfile.cjs +2035 -0
  4. package/dist/mailfile.js +2037 -0
  5. package/dist/mailfile.min.js +30 -0
  6. package/package.json +84 -0
  7. package/src/cfb/index.js +4 -0
  8. package/src/cfb/read.js +221 -0
  9. package/src/cfb/write.js +262 -0
  10. package/src/errors.js +49 -0
  11. package/src/index.js +86 -0
  12. package/src/mapi/bag.js +195 -0
  13. package/src/mapi/index.js +6 -0
  14. package/src/mapi/tags.js +147 -0
  15. package/src/message.js +199 -0
  16. package/src/mime/build.js +100 -0
  17. package/src/mime/encodings.js +188 -0
  18. package/src/mime/headers.js +344 -0
  19. package/src/mime/index.js +12 -0
  20. package/src/mime/parse.js +153 -0
  21. package/src/msg/read.js +178 -0
  22. package/src/msg/write.js +124 -0
  23. package/src/rtf/deencapsulate.js +155 -0
  24. package/src/rtf/index.js +3 -0
  25. package/src/rtf/lzfu.js +67 -0
  26. package/src/util.js +59 -0
  27. package/types/cfb/index.d.ts +2 -0
  28. package/types/cfb/read.d.ts +70 -0
  29. package/types/cfb/write.d.ts +67 -0
  30. package/types/errors.d.ts +36 -0
  31. package/types/index.d.ts +73 -0
  32. package/types/mapi/bag.d.ts +77 -0
  33. package/types/mapi/index.d.ts +2 -0
  34. package/types/mapi/tags.d.ts +137 -0
  35. package/types/message.d.ts +94 -0
  36. package/types/mime/build.d.ts +27 -0
  37. package/types/mime/encodings.d.ts +52 -0
  38. package/types/mime/headers.d.ts +111 -0
  39. package/types/mime/index.d.ts +4 -0
  40. package/types/mime/parse.d.ts +43 -0
  41. package/types/msg/read.d.ts +26 -0
  42. package/types/msg/write.d.ts +6 -0
  43. package/types/rtf/deencapsulate.d.ts +23 -0
  44. package/types/rtf/index.d.ts +2 -0
  45. package/types/rtf/lzfu.d.ts +8 -0
  46. package/types/util.d.ts +25 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Scott McKellar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,203 @@
1
+ # mailfile
2
+
3
+ Read and write Outlook `.msg` and RFC 5322 `.eml` files. Zero dependencies,
4
+ one small model in the middle, and it runs wherever `TextDecoder` does —
5
+ browsers, Node, Deno, Bun, Cloudflare Workers.
6
+
7
+ ```
8
+ .msg ──► [cfb] ──► [mapi] ──┐ ┌──► [mapi] ──► [cfb] ──► .msg
9
+ ├──► Message ───┤
10
+ .eml ──────────► [mime] ────┘ └──────────────[mime] ──► .eml
11
+
12
+ [rtf] recovers the HTML body when a .msg has no PR_HTML
13
+ ```
14
+
15
+ Two parsers in, two serializers out. The `Message` between them is plain and
16
+ mutable, so this is a toolkit as much as a converter: extract attachments,
17
+ rewrite headers in bulk, redact recipients, index a mail archive.
18
+
19
+ ## Install
20
+
21
+ ```bash
22
+ npm install mailfile
23
+ ```
24
+
25
+ Or drop it straight into a page — no build step, no bundler:
26
+
27
+ ```html
28
+ <script src="https://cdn.jsdelivr.net/npm/mailfile@0/dist/mailfile.min.js"></script>
29
+ <script>
30
+ const { data, format } = mailfile.convert(bytes, { filename: 'note.msg' })
31
+ </script>
32
+ ```
33
+
34
+ ## Convert
35
+
36
+ The common case is one call. The input format is detected from its magic
37
+ bytes, so a `.msg` someone renamed to `.eml` is still handled correctly.
38
+
39
+ ```js
40
+ import { convert } from 'mailfile'
41
+
42
+ const { data, format, message } = convert(bytes, { filename: 'report.msg' })
43
+ // data -> Uint8Array of .eml bytes
44
+ // format -> 'eml'
45
+ // message.subject, message.attachments, ...
46
+ ```
47
+
48
+ Pass `to` when you want a specific format rather than the other one:
49
+
50
+ ```js
51
+ convert(bytes, { to: 'eml' }) // normalise anything to .eml
52
+ ```
53
+
54
+ ## The message model
55
+
56
+ ```js
57
+ import { Message } from 'mailfile'
58
+
59
+ const m = Message.fromMsg(bytes) // or Message.fromEml(bytes)
60
+
61
+ m.subject // 'Quarterly résumé — café'
62
+ m.from // { name: 'Alice Åberg', email: 'alice@example.com' }
63
+ m.to // [{ name, email }, ...] also .cc and .bcc
64
+ m.date // Date | null
65
+ m.text // plain-text body
66
+ m.html // HTML body, recovered from compressed RTF if needed
67
+ m.attachments // [{ filename, mime, data, cid, inline }]
68
+ m.headers // ordered header multimap, see below
69
+
70
+ m.subject = 'Re: ' + m.subject
71
+ m.attachments = m.attachments.filter(a => !a.inline)
72
+
73
+ const eml = m.toEml()
74
+ const msg = m.toMsg()
75
+ ```
76
+
77
+ Headers keep their order and their repeats:
78
+
79
+ ```js
80
+ m.headers.get('subject') // first value, case-insensitive
81
+ m.headers.getAll('received') // every hop, in order
82
+ m.headers.set('X-Reviewed', 'yes')
83
+ m.headers.raw // the block as text, folded
84
+ ```
85
+
86
+ Original internet headers survive a `.msg` → `.eml` conversion when the
87
+ message carried them, so a `.msg` that started life as email keeps its full
88
+ `Received:` trail. Content headers are always regenerated to match the body
89
+ actually written.
90
+
91
+ ### Reading only what you need
92
+
93
+ `lazy` defers pulling attachment bytes out of the compound file until you
94
+ touch `.data` — worth it when you are indexing headers across a large archive.
95
+
96
+ ```js
97
+ const m = Message.fromMsg(bytes, { lazy: true })
98
+ m.subject // cheap
99
+ m.attachments[0].data // extracted now
100
+ ```
101
+
102
+ ### Properties the model does not cover
103
+
104
+ `.msg` is a MAPI property bag, and this library models the mail-shaped subset.
105
+ Anything else is still reachable — appointments, contacts, custom properties:
106
+
107
+ ```js
108
+ const m = Message.fromMsg(bytes)
109
+ m.messageClass // 'IPM.Appointment'
110
+ m.props.str(0x0037) // PR_SUBJECT
111
+ m.props.int(0x0E07) // PR_MESSAGE_FLAGS
112
+ m.props.date(0x0060) // PR_START_DATE
113
+ m.props.ids() // every property id present
114
+ ```
115
+
116
+ ## The pieces, separately
117
+
118
+ Each layer is independently useful and independently importable, so you only
119
+ pay for what you use.
120
+
121
+ ```js
122
+ import { read, write, newRoot, addStream } from 'mailfile/cfb'
123
+ import { parseEml, buildEml, Headers } from 'mailfile/mime'
124
+ import { PropertyBag, PropId } from 'mailfile/mapi'
125
+ import { decompress, rtfToHtml } from 'mailfile/rtf'
126
+ ```
127
+
128
+ `mailfile/cfb` is a complete OLE2 compound-file reader **and** writer. It has
129
+ nothing to do with email — point it at a legacy `.doc`, `.xls`, `.ppt`, an
130
+ `.msi`, or a `Thumbs.db`:
131
+
132
+ ```js
133
+ const cfb = read(bytes)
134
+ for (const entry of cfb.childrenOf(cfb.root).list) {
135
+ console.log(entry.name, entry.size)
136
+ }
137
+ ```
138
+
139
+ ## Errors
140
+
141
+ Every failure is a `MailfileError` carrying a `code`, so you can tell "wrong
142
+ kind of file" apart from "damaged file" without matching on message text.
143
+
144
+ ```js
145
+ import { MailfileError, ErrorCode } from 'mailfile'
146
+
147
+ try {
148
+ convert(bytes)
149
+ } catch (err) {
150
+ if (err.code === ErrorCode.NOT_COMPOUND_FILE) // not really a .msg
151
+ if (err.code === ErrorCode.CORRUPT_CFB) // damaged
152
+ }
153
+ ```
154
+
155
+ Parsing is deliberately forgiving — real mail is malformed constantly, and a
156
+ mislabelled charset or a missing closing boundary should not cost you the
157
+ message.
158
+
159
+ ## What carries across
160
+
161
+ Headers, sender and recipients, subject and date, plain-text and HTML bodies,
162
+ inline images by content ID, and every attachment byte for byte. HTML stored
163
+ only as compressed RTF (`PR_RTF_COMPRESSED`) is de-encapsulated back to HTML,
164
+ which is how a lot of Outlook-authored mail stores it.
165
+
166
+ ## Known gaps
167
+
168
+ - **Named properties.** `__nameid_version1.0` is written empty and not parsed,
169
+ so categories and custom named properties do not survive a write.
170
+ - **Embedded messages** are read (they become nested `.eml` attachments) but
171
+ written back by value, not as true embedded messages.
172
+ - **`PR_RTF_COMPRESSED` is never generated.** Outlook regenerates it on open;
173
+ stricter MAPI consumers may not.
174
+ - **TNEF (`winmail.dat`)** is not handled.
175
+ - **PST/OST** is out of scope and will stay that way.
176
+
177
+ ## Testing
178
+
179
+ ```bash
180
+ npm test # unit tests
181
+ node scripts/corpus.js --eml ~/mail --any # your own mail, end to end
182
+ ```
183
+
184
+ The corpus runner takes any directory tree of real messages, pushes each one
185
+ through `eml → msg → eml`, compares field by field, and has an unrelated MSG
186
+ reader check everything written. It is the check that finds real bugs; bring
187
+ your own archive, since none ships here.
188
+
189
+ ## Implementation notes
190
+
191
+ Written against the published Microsoft open specifications — [MS-CFB]
192
+ (compound file), [MS-OXMSG] (`.msg`), [MS-OXRTFCP] (RTF compression) and
193
+ [MS-OXRTFEX] (HTML encapsulation) — with no code derived from an existing
194
+ implementation.
195
+
196
+ [MS-CFB]: https://learn.microsoft.com/openspecs/windows_protocols/ms-cfb/
197
+ [MS-OXMSG]: https://learn.microsoft.com/openspecs/exchange_server_protocols/ms-oxmsg/
198
+ [MS-OXRTFCP]: https://learn.microsoft.com/openspecs/exchange_server_protocols/ms-oxrtfcp/
199
+ [MS-OXRTFEX]: https://learn.microsoft.com/openspecs/exchange_server_protocols/ms-oxrtfex/
200
+
201
+ ## License
202
+
203
+ MIT