postal-mime 2.0.2 → 2.1.1-patch.1

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.1.1](https://github.com/postalsys/postal-mime/compare/v2.1.0...v2.1.1) (2024-02-26)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * **types:** Updated types for PostalMime ([bc90f6d](https://github.com/postalsys/postal-mime/commit/bc90f6d5b7d3e2475cece77bb094caf421dead97))
9
+
10
+ ## [2.1.0](https://github.com/postalsys/postal-mime/compare/v2.0.2...v2.1.0) (2024-02-22)
11
+
12
+
13
+ ### Features
14
+
15
+ * **workers:** Support Cloudflare Email Workers out of the box ([4904708](https://github.com/postalsys/postal-mime/commit/49047089bf779931dacb4a7b31816b48d1b00840))
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * **module:** add types to module exports ([#23](https://github.com/postalsys/postal-mime/issues/23)) ([1ee4a42](https://github.com/postalsys/postal-mime/commit/1ee4a427643d71f6a4bda0db0ebe0b5b280e52ae))
21
+
3
22
  ## [2.0.2](https://github.com/postalsys/postal-mime/compare/v2.0.1...v2.0.2) (2023-12-08)
4
23
 
5
24
 
package/README.md CHANGED
@@ -1,111 +1,6 @@
1
1
  # postal-mime
2
2
 
3
- Email parser for browser environments.
3
+ `postal-mime` is now `@postalsys/mime`
4
4
 
5
- PostalMime can be run in the main web thread or from Web Workers.
6
-
7
- ## Source
8
-
9
- Source code is available from [Github](https://github.com/postalsys/postal-mime).
10
-
11
- ## Demo
12
-
13
- See this [example](https://kreata.ee/postal-mime/example/).
14
-
15
- ## Usage
16
-
17
- First install the module from npm:
18
-
19
- ```
20
- $ npm install postal-mime
21
- ```
22
-
23
- next import the PostalMime class into your script:
24
-
25
- ```js
26
- import PostalMime from './node_modules/postal-mime/src/postal-mime.js';
27
- ```
28
-
29
- or when using from a Node.js app
30
-
31
- ```js
32
- import PostalMime from 'postal-mime';
33
- ```
34
-
35
- ### Promises
36
-
37
- All postal-mime methods use Promises, so you need to wait using `await` or wait for the `then()` method to fire until you get the response.
38
-
39
- #### Browser
40
-
41
- ```js
42
- import PostalMime from './node_modules/postal-mime/src/postal-mime.js';
43
-
44
- const parser = new PostalMime();
45
- const email = await parser.parse(`Subject: My awesome email 🤓
46
- Content-Type: text/html; charset=utf-8
47
-
48
- <p>Hello world 😵‍💫</p>`);
49
-
50
- console.log(email.subject);
51
- ```
52
-
53
- #### Node.js
54
-
55
- It is pretty much the same as in the browser.
56
-
57
- ```js
58
- import PostalMime from 'postal-mime';
59
- import util from 'node:util';
60
-
61
- const parser = new PostalMime();
62
- const email = await parser.parse(`Subject: My awesome email 🤓
63
- Content-Type: text/html; charset=utf-8
64
-
65
- <p>Hello world 😵‍💫</p>`);
66
-
67
- console.log(util.inspect(email, false, 22, true));
68
- ```
69
-
70
- #### parser.parse()
71
-
72
- ```js
73
- parser.parse(email) -> Promise
74
- ```
75
-
76
- Where
77
-
78
- - **email** is the rfc822 formatted email. Either a string, an ArrayBuffer, a Blob object or a Node.js Buffer
79
-
80
- > **NB** you can call `parse()` only once. If you need to parse another message, create a new _PostalMime_ object.
81
-
82
- This method parses an email message into a structured object with the following properties:
83
-
84
- - **headers** is an array of headers in the same order as found from the message (topmost headers first).
85
- - **headers[].key** is lowercase key of the header line, eg. `"dkim-signature"`
86
- - **headers[].value** is the unprocessed value of the header line
87
- - **from**, **sender**, **replyTo** includes a processed object for the corresponding headers
88
- - **from.name** is decoded name (empty string if not set)
89
- - **from.address** is the email address
90
- - **deliveredTo**, **returnPath** is the email address from the corresponding header
91
- - **to**, **cc**, **bcc** includes an array of processed objects for the corresponding headers
92
- - **to[].name** is decoded name (empty string if not set)
93
- - **to[].address** is the email address
94
- - **subject** is the email subject line
95
- - **messageId**, **inReplyTo**, **references** includes the value as found from the corresponding header without any processing
96
- - **date** is the email sending time formatted as an ISO date string (unless parsing failed and in this case the original value is used)
97
- - **html** is the HTML content of the message as a string
98
- - **text** is the plaintext content of the message as a string
99
- - **attachments** is an array that includes message attachments
100
- - **attachment[].filename** is the file name if provided
101
- - **attachment[].mimeType** is the MIME type of the attachment
102
- - **attachment[].disposition** is either "attachment", "inline" or `null` if disposition was not provided
103
- - **attachment[].related** is a boolean value that indicats if this attachment should be treated as embedded image
104
- - **attachment[].contentId** is the ID from Content-ID header
105
- - **attachment[].content** is an ArrayBuffer that contains the attachment file
106
-
107
- ## License
108
-
109
- &copy; 2021-2023 Andris Reinman
110
-
111
- `postal-mime` is licensed under the **MIT No Attribution license**
5
+ - @postalsys/mime in [Github](https://github.com/postalsys/mime)
6
+ - @postalsys/mime in [NPM](https://www.npmjs.com/package/@postalsys/mime)
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "postal-mime",
3
- "version": "2.0.2",
3
+ "version": "2.1.1-patch.1",
4
4
  "description": "Email parser for browser environments",
5
5
  "main": "./src/postal-mime.js",
6
6
  "exports": {
7
- "import": "./src/postal-mime.js"
7
+ "import": "./src/postal-mime.js",
8
+ "types": "./postal-mime.d.ts"
8
9
  },
9
10
  "type": "module",
10
11
  "types": "postal-mime.d.ts",
@@ -26,10 +27,11 @@
26
27
  "author": "Andris Reinman",
27
28
  "license": "MIT-0",
28
29
  "devDependencies": {
30
+ "@types/node": "20.11.20",
29
31
  "cross-blob": "3.0.2",
30
- "eslint": "8.55.0",
32
+ "cross-env": "7.0.3",
33
+ "eslint": "8.57.0",
31
34
  "eslint-cli": "1.1.1",
32
- "iframe-resizer": "4.3.9",
33
- "cross-env": "7.0.3"
35
+ "iframe-resizer": "4.3.9"
34
36
  }
35
37
  }
package/postal-mime.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export type RawEmail = string | ArrayBuffer | Blob | Buffer;
1
+ export type RawEmail = string | ArrayBuffer | Uint8Array | Blob | Buffer | ReadableStream;
2
2
 
3
3
  export type Header = Record<string, string>;
4
4
 
@@ -13,7 +13,7 @@ export type Attachment = {
13
13
  disposition: "attachment" | "inline" | null;
14
14
  related?: boolean;
15
15
  contentId?: string;
16
- content: ArrayBuffer;
16
+ content: Uint8Array;
17
17
  };
18
18
 
19
19
  export type Email = {
@@ -287,31 +287,61 @@ export default class PostalMime {
287
287
  this.textContent = textContent;
288
288
  }
289
289
 
290
+ async resolveStream(stream) {
291
+ let chunkLen = 0;
292
+ let chunks = [];
293
+ const reader = stream.getReader();
294
+
295
+ while (true) {
296
+ const { done, value } = await reader.read();
297
+ if (done) {
298
+ break;
299
+ }
300
+ chunks.push(value);
301
+ chunkLen += value.length;
302
+ }
303
+
304
+ const result = new Uint8Array(chunkLen);
305
+ let chunkPointer = 0;
306
+ for (let chunk of chunks) {
307
+ result.set(chunk, chunkPointer);
308
+ chunkPointer += chunk.length;
309
+ }
310
+
311
+ return result;
312
+ }
313
+
290
314
  async parse(buf) {
291
315
  if (this.started) {
292
316
  throw new Error('Can not reuse parser, create a new PostalMime object');
293
317
  }
294
318
  this.started = true;
295
319
 
296
- // should it thrown on empty value instead?
320
+ // Check if the input is a readable stream and resolve it into an ArrayBuffer
321
+ if (buf && typeof buf.getReader === 'function') {
322
+ buf = await this.resolveStream(buf);
323
+ }
324
+
325
+ // Should it throw for an empty value instead of defaulting to an empty ArrayBuffer?
297
326
  buf = buf || ArrayBuffer(0);
298
327
 
328
+ // Cast string input to Uint8Array
299
329
  if (typeof buf === 'string') {
300
- // cast string input to ArrayBuffer
301
330
  buf = textEncoder.encode(buf);
302
331
  }
303
332
 
333
+ // Cast Blob to ArrayBuffer
304
334
  if (buf instanceof Blob || Object.prototype.toString.call(buf) === '[object Blob]') {
305
- // can't process blob directly, cast to ArrayBuffer
306
335
  buf = await blobToArrayBuffer(buf);
307
336
  }
308
337
 
338
+ // Cast Node.js Buffer object or Uint8Array into ArrayBuffer
309
339
  if (buf.buffer instanceof ArrayBuffer) {
310
- // Node.js Buffer object or Uint8Array
311
340
  buf = new Uint8Array(buf).buffer;
312
341
  }
313
342
 
314
343
  this.buf = buf;
344
+
315
345
  this.av = new Uint8Array(buf);
316
346
  this.readPos = 0;
317
347