postal-mime
Advanced tools
Comparing version 2.0.2 to 2.1.0
# Changelog | ||
## [2.1.0](https://github.com/postalsys/postal-mime/compare/v2.0.2...v2.1.0) (2024-02-22) | ||
### Features | ||
* **workers:** Support Cloudflare Email Workers out of the box ([4904708](https://github.com/postalsys/postal-mime/commit/49047089bf779931dacb4a7b31816b48d1b00840)) | ||
### Bug Fixes | ||
* **module:** add types to module exports ([#23](https://github.com/postalsys/postal-mime/issues/23)) ([1ee4a42](https://github.com/postalsys/postal-mime/commit/1ee4a427643d71f6a4bda0db0ebe0b5b280e52ae)) | ||
## [2.0.2](https://github.com/postalsys/postal-mime/compare/v2.0.1...v2.0.2) (2023-12-08) | ||
@@ -4,0 +16,0 @@ |
{ | ||
"name": "postal-mime", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "Email parser for browser environments", | ||
"main": "./src/postal-mime.js", | ||
"exports": { | ||
"import": "./src/postal-mime.js" | ||
"import": "./src/postal-mime.js", | ||
"types": "./postal-mime.d.ts" | ||
}, | ||
@@ -30,3 +31,3 @@ "type": "module", | ||
"cross-blob": "3.0.2", | ||
"eslint": "8.55.0", | ||
"eslint": "8.56.0", | ||
"eslint-cli": "1.1.1", | ||
@@ -33,0 +34,0 @@ "iframe-resizer": "4.3.9", |
@@ -70,2 +70,21 @@ # postal-mime | ||
#### Cloudflare [Email Workers](https://developers.cloudflare.com/email-routing/email-workers/) | ||
Pretty much the same as in Node.js. Use `message.raw` as the raw message for parsing. | ||
```js | ||
import PostalMime from 'postal-mime'; | ||
export default { | ||
async email(message, env, ctx) { | ||
const parser = new PostalMime(); | ||
const email = await parser.parse(message.raw); | ||
console.log('Subject: ', email.subject); | ||
console.log('HTML: ', email.html); | ||
console.log('Text: ', email.text); | ||
} | ||
}; | ||
``` | ||
#### parser.parse() | ||
@@ -72,0 +91,0 @@ |
@@ -290,2 +290,26 @@ import MimeNode from './mime-node.js'; | ||
async resolveStream(stream) { | ||
let chunkLen = 0; | ||
let chunks = []; | ||
const reader = stream.getReader(); | ||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) { | ||
break; | ||
} | ||
chunks.push(value); | ||
chunkLen += value.length; | ||
} | ||
const result = new Uint8Array(chunkLen); | ||
let chunkPointer = 0; | ||
for (let chunk of chunks) { | ||
result.set(chunk, chunkPointer); | ||
chunkPointer += chunk.length; | ||
} | ||
return result; | ||
} | ||
async parse(buf) { | ||
@@ -297,2 +321,7 @@ if (this.started) { | ||
// check if the input is a readable stream and resolve it into an ArrayBuffer | ||
if (buf && typeof buf.getReader === 'function') { | ||
buf = await this.resolveStream(buf); | ||
} | ||
// should it thrown on empty value instead? | ||
@@ -299,0 +328,0 @@ buf = buf || ArrayBuffer(0); |
126981
3792
131