Skip to content

Commit

Permalink
v1.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Jan 15, 2021
1 parent 4778dad commit 7ca5e9e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 8 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ console.log(email.subject);
console.log(email.html);
```

### Node.js

Even though PostalMime is built for the browser environment you can also use it in Node.js with a few tweaks. Notably you'd need to register a global _Blob_ class that is not available by default in Node.

```js
// Set up global Blob
// $ npm install cross-blob
globalThis.Blob = require('cross-blob');
// Require Node.js version of the library
const PostalMime = require('postal-mime/dist/node').postalMime.default;

// Use PostalMime as you'd normally do
new PostalMime().parse('Subject: test').then(res => console.log(res));
```

#### parser.parse()

```js
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postal-mime",
"version": "1.0.5",
"version": "1.0.6",
"description": "Email parser for browser environments",
"main": "dist/postal-mime.js",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions src/decode-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export function getDecoder(charset) {
* @returns {ArrayBuffer} Converted value
*/
export async function blobToArrayBuffer(blob) {
if ('arrayBuffer' in blob) {
return await blob.arrayBuffer();
}

const fr = new FileReader();

return new Promise((resolve, reject) => {
Expand Down
12 changes: 8 additions & 4 deletions src/mime-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export default class MimeNode {
return;
}

if (this.state === 'header') {
this.processHeaders();
}

// remove self from boundary listing
let boundaries = this.parser.boundaries;
for (let i = boundaries.length - 1; i >= 0; i--) {
Expand All @@ -64,7 +68,7 @@ export default class MimeNode {

await this.finalizeChildNodes();

this.content = await this.contentDecoder.finalize();
this.content = this.contentDecoder ? await this.contentDecoder.finalize() : null;

this.state = 'finished';
}
Expand Down Expand Up @@ -208,13 +212,13 @@ export default class MimeNode {

switch (key.toLowerCase()) {
case 'content-type':
this.contentType = { value };
this.contentType = { value, parsed: {} };
break;
case 'content-transfer-encoding':
this.contentTransferEncoding = { value };
this.contentTransferEncoding = { value, parsed: {} };
break;
case 'content-disposition':
this.contentDisposition = { value };
this.contentDisposition = { value, parsed: {} };
break;
case 'content-id':
this.contentId = value;
Expand Down
2 changes: 1 addition & 1 deletion src/postal-mime.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export default class PostalMime {
buf = textEncoder.encode(buf);
}

if (buf instanceof Blob) {
if (buf instanceof Blob || Object.prototype.toString.call(buf) === '[object Blob]') {
// can't process blob directly, cast to ArrayBuffer
buf = await blobToArrayBuffer(buf);
}
Expand Down
18 changes: 16 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@ const path = require('path');

const mode = process.env.NODE_ENV === 'production' ? 'production' : 'development';

module.exports = {
const clientConfig = {
entry: './src/postal-mime.js',
mode,

target: 'web',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'postal-mime.js',
library: 'postalMime',
libraryTarget: 'umd'
}
};

const serverConfig = {
entry: './src/postal-mime.js',
mode,
target: 'node',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'node.js',
library: 'postalMime',
libraryTarget: 'commonjs'
}
};

module.exports = [serverConfig, clientConfig];

0 comments on commit 7ca5e9e

Please sign in to comment.