Skip to content

Commit

Permalink
fix(types): Updated types for PostalMime
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Feb 26, 2024
1 parent 470ec33 commit bc90f6d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
34 changes: 25 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
"author": "Andris Reinman",
"license": "MIT-0",
"devDependencies": {
"@types/node": "20.11.20",
"cross-blob": "3.0.2",
"eslint": "8.56.0",
"cross-env": "7.0.3",
"eslint": "8.57.0",
"eslint-cli": "1.1.1",
"iframe-resizer": "4.3.9",
"cross-env": "7.0.3"
"iframe-resizer": "4.3.9"
}
}
4 changes: 2 additions & 2 deletions postal-mime.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type RawEmail = string | ArrayBuffer | Blob | Buffer;
export type RawEmail = string | ArrayBuffer | Uint8Array | Blob | Buffer | ReadableStream;

export type Header = Record<string, string>;

Expand All @@ -13,7 +13,7 @@ export type Attachment = {
disposition: "attachment" | "inline" | null;
related?: boolean;
contentId?: string;
content: ArrayBuffer;
content: Uint8Array;
};

export type Email = {
Expand Down
11 changes: 6 additions & 5 deletions src/postal-mime.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,30 +317,31 @@ export default class PostalMime {
}
this.started = true;

// check if the input is a readable stream and resolve it into an ArrayBuffer
// 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?
// Should it throw for an empty value instead of defaulting to an empty ArrayBuffer?
buf = buf || ArrayBuffer(0);

// Cast string input to Uint8Array
if (typeof buf === 'string') {
// cast string input to ArrayBuffer
buf = textEncoder.encode(buf);
}

// Cast Blob to ArrayBuffer
if (buf instanceof Blob || Object.prototype.toString.call(buf) === '[object Blob]') {
// can't process blob directly, cast to ArrayBuffer
buf = await blobToArrayBuffer(buf);
}

// Cast Node.js Buffer object or Uint8Array into ArrayBuffer
if (buf.buffer instanceof ArrayBuffer) {
// Node.js Buffer object or Uint8Array
buf = new Uint8Array(buf).buffer;
}

this.buf = buf;

this.av = new Uint8Array(buf);
this.readPos = 0;

Expand Down
1 change: 1 addition & 0 deletions test/postal-mime-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from 'node:buffer';
import test from 'node:test';
import assert from 'node:assert';
import { readFile } from 'node:fs/promises';
Expand Down

0 comments on commit bc90f6d

Please sign in to comment.