readpst 0.1.0 → 0.2.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.
package/README.md CHANGED
@@ -25,17 +25,35 @@ const files = await extract("mailbox.pst", { outDir: "out" });
25
25
  // ["/abs/out/Inbox/1.eml", "/abs/out/Sent Items/1.eml", ...]
26
26
  ```
27
27
 
28
- `outDir` mirrors the PST's folder tree, with one RFC 822 `.eml` per message and attachments kept inline as MIME parts. Text is written as UTF-8.
28
+ ### Without managing files
29
+
30
+ `read` extracts into a temp dir, yields each item and removes the dir when the loop ends (also on `break` or a thrown error):
31
+
32
+ ```js
33
+ import { read } from "readpst";
34
+
35
+ for await (const item of read("mailbox.pst")) {
36
+ item.path; // "Inbox/1.eml"
37
+ await item.buffer(); // Buffer
38
+ item.stream(); // fs.ReadStream, e.g. to upload to S3
39
+ }
40
+ ```
41
+
42
+ Consume each item's buffer or stream before moving to the next one; the files are gone once the loop finishes.
43
+
44
+ ### Writing to a directory
45
+
46
+ `extract` writes to a directory you own and returns the file paths. `outDir` mirrors the PST's folder tree, with one RFC 822 `.eml` per message and attachments kept inline as MIME parts. Text is written as UTF-8.
29
47
 
30
48
  ### Options
31
49
 
32
50
  | Option | Default | Description |
33
51
  | ------------- | ------------- | ----------------------------------------------------------------- |
34
- | `outDir` | (required) | Output directory, created if missing |
52
+ | `outDir` | (required) | `extract` only: output directory, created if missing |
35
53
  | `types` | `["email"]` | Any of `"email"`, `"appointment"`, `"journal"`, `"contact"` |
36
54
  | `readpstPath` | `"readpst"` | Path to the binary |
37
55
 
38
- `readpst` needs a seekable file on local disk, so download a remote PST to a temp file first. Cleaning up `outDir` is up to you.
56
+ `readpst` needs a seekable file on local disk, so download a remote PST to a temp file first. With `extract`, cleaning up `outDir` is up to you.
39
57
 
40
58
  ## License
41
59
 
package/index.d.ts CHANGED
@@ -11,3 +11,16 @@ export interface ExtractOptions {
11
11
 
12
12
  /** Converts a .pst/.ost into one .eml per item and resolves to the absolute paths of the written files. */
13
13
  export function extract(pstPath: string, options: ExtractOptions): Promise<string[]>;
14
+
15
+ export interface PstItem {
16
+ /** Path relative to the PST root, e.g. "Inbox/1.eml". */
17
+ path: string;
18
+ buffer(): Promise<Buffer>;
19
+ stream(): import("node:fs").ReadStream;
20
+ }
21
+
22
+ /**
23
+ * Extracts into a temp dir and yields each item; the dir is removed when the loop ends, breaks or throws.
24
+ * Read an item's buffer or stream before moving to the next one.
25
+ */
26
+ export function read(pstPath: string, options?: Omit<ExtractOptions, "outDir">): AsyncGenerator<PstItem, void, undefined>;
package/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { execFile } from "node:child_process";
2
- import { mkdir, readdir } from "node:fs/promises";
3
- import { join, resolve } from "node:path";
2
+ import { createReadStream } from "node:fs";
3
+ import { mkdir, mkdtemp, readFile, readdir, rm } from "node:fs/promises";
4
+ import { tmpdir } from "node:os";
5
+ import { join, relative, resolve } from "node:path";
4
6
  import { promisify } from "node:util";
5
7
 
6
8
  const run = promisify(execFile);
@@ -30,3 +32,14 @@ export async function extract(pstPath, { outDir, types = ["email"], readpstPath
30
32
  const entries = await readdir(dir, { recursive: true, withFileTypes: true });
31
33
  return entries.filter((entry) => entry.isFile()).map((entry) => join(entry.parentPath, entry.name));
32
34
  }
35
+
36
+ export async function* read(pstPath, options = {}) {
37
+ const dir = await mkdtemp(join(tmpdir(), "readpst-"));
38
+ try {
39
+ for (const file of await extract(pstPath, { ...options, outDir: dir })) {
40
+ yield { path: relative(dir, file), buffer: () => readFile(file), stream: () => createReadStream(file) };
41
+ }
42
+ } finally {
43
+ await rm(dir, { recursive: true, force: true });
44
+ }
45
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "readpst",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Convert Outlook .pst files to .eml files with libpst's readpst",
5
5
  "keywords": ["pst", "ost", "outlook", "eml", "email", "readpst", "libpst"],
6
6
  "author": "Philip van Burleigh",