@zone-eu/mailsplit 5.4.9 → 5.4.11

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/index.d.ts ADDED
@@ -0,0 +1,100 @@
1
+ /// <reference types="node" />
2
+
3
+ import Splitter = require('./lib/message-splitter');
4
+ import Joiner = require('./lib/message-joiner');
5
+ import Rewriter = require('./lib/node-rewriter');
6
+ import Streamer = require('./lib/node-streamer');
7
+ import ChunkedPassthrough = require('./lib/chunked-passthrough');
8
+ import Headers = require('./lib/headers');
9
+ import MimeNode = require('./lib/mime-node');
10
+
11
+ export {
12
+ /** Splits raw message bytes into MIME node and content chunks. */
13
+ Splitter,
14
+
15
+ /** Joins MIME node and content chunks back into raw message bytes. */
16
+ Joiner,
17
+
18
+ /** Rewrites body content for MIME nodes selected by a filter function. */
19
+ Rewriter,
20
+
21
+ /** Streams decoded body content for MIME nodes selected by a filter function. */
22
+ Streamer,
23
+
24
+ /** Buffers byte input and emits larger Buffer chunks. */
25
+ ChunkedPassthrough,
26
+
27
+ /** Parses, mutates, and rebuilds message header blocks. */
28
+ Headers,
29
+
30
+ /** Represents one parsed MIME node and its header/body metadata. */
31
+ MimeNode
32
+ };
33
+
34
+ export type {
35
+ /** Value that is either present as `T` or explicitly unavailable as `false`. */
36
+ Maybe,
37
+
38
+ /** Single item in an IMAP-style MIME part number. */
39
+ PartNumberItem,
40
+
41
+ /** IMAP-style path to a MIME part. */
42
+ PartNumber,
43
+
44
+ /** Options passed through to libmime instances. */
45
+ LibmimeOptions,
46
+
47
+ /** Configuration for `Splitter` and MIME node parsing. */
48
+ SplitterOptions,
49
+
50
+ /** Configuration for `ChunkedPassthrough`. */
51
+ ChunkedPassthroughOptions,
52
+
53
+ /** Configuration for format=flowed decoding. */
54
+ FlowedDecoderOptions,
55
+
56
+ /** Parsed raw header line with a normalized lookup key. */
57
+ HeaderLine,
58
+
59
+ /** Decoded structured header value. */
60
+ DecodedHeader,
61
+
62
+ /** MIME node shape emitted by `Splitter`. */
63
+ MimeNode,
64
+
65
+ /** Data or body bytes emitted by `Splitter`. */
66
+ MessageChunk,
67
+
68
+ /** Sentinel input used internally by rewriter/streamer transforms. */
69
+ EmptyChunk,
70
+
71
+ /** Object emitted by `Splitter`. */
72
+ SplitterChunk,
73
+
74
+ /** Object accepted by rewriter and streamer transforms. */
75
+ RewriterInput,
76
+
77
+ /** Predicate used to select MIME nodes. */
78
+ FilterFunc,
79
+
80
+ /** Error object that may include a Node-style string code. */
81
+ ErrorWithCode,
82
+
83
+ /** Callback that resumes processing after a selected node stream ends. */
84
+ ContinueCallback,
85
+
86
+ /** Content transform stream used for decoded or encoded node bodies. */
87
+ ContentStream,
88
+
89
+ /** Decoder stream with an internal readable-state guard. */
90
+ DecoderStream,
91
+
92
+ /** Internal splitter grouping state. */
93
+ SplitterGroup,
94
+
95
+ /** Payload emitted with `Rewriter`'s `node` event. */
96
+ RewriterNode,
97
+
98
+ /** Payload emitted with `Streamer`'s `node` event. */
99
+ StreamerNode
100
+ } from './lib/types';
package/index.js CHANGED
@@ -6,12 +6,15 @@ const NodeRewriter = require('./lib/node-rewriter');
6
6
  const NodeStreamer = require('./lib/node-streamer');
7
7
  const Headers = require('./lib/headers');
8
8
  const ChunkedPassthrough = require('./lib/chunked-passthrough');
9
+ const MimeNode = require('./lib/mime-node');
9
10
 
11
+ /** @type {typeof import('.')} */
10
12
  module.exports = {
11
13
  Splitter: MessageSplitter,
12
14
  Joiner: MessageJoiner,
13
15
  Rewriter: NodeRewriter,
14
16
  Streamer: NodeStreamer,
15
17
  ChunkedPassthrough,
16
- Headers
18
+ Headers,
19
+ MimeNode
17
20
  };
@@ -0,0 +1,59 @@
1
+ import { Transform } from 'node:stream';
2
+ import type { ChunkedPassthroughOptions } from './types';
3
+
4
+ /** Transform stream that buffers byte input and emits larger Buffer chunks. */
5
+ declare class ChunkedPassthrough extends Transform {
6
+ /**
7
+ * Creates a chunking passthrough transform that accepts Buffer input and emits Buffer chunks.
8
+ *
9
+ * @param options Optional chunk size configuration.
10
+ */
11
+ constructor(options?: ChunkedPassthroughOptions);
12
+
13
+ /**
14
+ * Registers a listener for buffered byte chunks.
15
+ *
16
+ * @param event Event name.
17
+ * @param listener Receives each buffered Buffer chunk.
18
+ * @returns This passthrough instance.
19
+ */
20
+ on(event: 'data', listener: (data: Buffer) => void): this;
21
+
22
+ /**
23
+ * Registers a one-time listener for the next buffered byte chunk.
24
+ *
25
+ * @param event Event name.
26
+ * @param listener Receives the next buffered Buffer chunk.
27
+ * @returns This passthrough instance.
28
+ */
29
+ once(event: 'data', listener: (data: Buffer) => void): this;
30
+
31
+ /**
32
+ * Adds a listener for buffered byte chunks.
33
+ *
34
+ * @param event Event name.
35
+ * @param listener Receives each buffered Buffer chunk.
36
+ * @returns This passthrough instance.
37
+ */
38
+ addListener(event: 'data', listener: (data: Buffer) => void): this;
39
+
40
+ /**
41
+ * Prepends a listener for buffered byte chunks.
42
+ *
43
+ * @param event Event name.
44
+ * @param listener Receives each buffered Buffer chunk.
45
+ * @returns This passthrough instance.
46
+ */
47
+ prependListener(event: 'data', listener: (data: Buffer) => void): this;
48
+
49
+ /**
50
+ * Emits a buffered byte chunk.
51
+ *
52
+ * @param event Event name.
53
+ * @param data Buffer chunk to emit.
54
+ * @returns `true` when the event had listeners.
55
+ */
56
+ emit(event: 'data', data: Buffer): boolean;
57
+ }
58
+
59
+ export = ChunkedPassthrough;
@@ -2,7 +2,13 @@
2
2
 
3
3
  const { Transform } = require('stream');
4
4
 
5
+ /**
6
+ * Transform stream that buffers byte input and emits larger Buffer chunks.
7
+ */
5
8
  class ChunkedPassthrough extends Transform {
9
+ /**
10
+ * @param {import('..').ChunkedPassthroughOptions} [options]
11
+ */
6
12
  constructor(options = {}) {
7
13
  let config = {
8
14
  readableObjectMode: true,
@@ -13,6 +19,12 @@ class ChunkedPassthrough extends Transform {
13
19
  this.buffer = Buffer.alloc(0);
14
20
  }
15
21
 
22
+ /**
23
+ * @param {Buffer} chunk
24
+ * @param {BufferEncoding} encoding
25
+ * @param {import('stream').TransformCallback} callback
26
+ * @returns {void}
27
+ */
16
28
  _transform(chunk, encoding, callback) {
17
29
  this.buffer = Buffer.concat([this.buffer, chunk]);
18
30
 
@@ -24,6 +36,10 @@ class ChunkedPassthrough extends Transform {
24
36
  callback();
25
37
  }
26
38
 
39
+ /**
40
+ * @param {import('stream').TransformCallback} callback
41
+ * @returns {void}
42
+ */
27
43
  _flush(callback) {
28
44
  // Send remaining data
29
45
  if (this.buffer.length > 0) {
@@ -0,0 +1,59 @@
1
+ import { Transform } from 'node:stream';
2
+ import type { FlowedDecoderOptions } from './types';
3
+
4
+ /** Transform stream that decodes `text/plain; format=flowed` content. */
5
+ declare class FlowedDecoder extends Transform {
6
+ /**
7
+ * Creates a flowed text decoder that accepts encoded text bytes and emits decoded Buffer chunks.
8
+ *
9
+ * @param config Optional flowed text and charset decoding settings.
10
+ */
11
+ constructor(config?: FlowedDecoderOptions);
12
+
13
+ /**
14
+ * Registers a listener for decoded flowed text bytes.
15
+ *
16
+ * @param event Event name.
17
+ * @param listener Receives decoded Buffer chunks.
18
+ * @returns This decoder instance.
19
+ */
20
+ on(event: 'data', listener: (data: Buffer) => void): this;
21
+
22
+ /**
23
+ * Registers a one-time listener for the next decoded flowed text chunk.
24
+ *
25
+ * @param event Event name.
26
+ * @param listener Receives the next decoded Buffer chunk.
27
+ * @returns This decoder instance.
28
+ */
29
+ once(event: 'data', listener: (data: Buffer) => void): this;
30
+
31
+ /**
32
+ * Adds a listener for decoded flowed text bytes.
33
+ *
34
+ * @param event Event name.
35
+ * @param listener Receives decoded Buffer chunks.
36
+ * @returns This decoder instance.
37
+ */
38
+ addListener(event: 'data', listener: (data: Buffer) => void): this;
39
+
40
+ /**
41
+ * Prepends a listener for decoded flowed text bytes.
42
+ *
43
+ * @param event Event name.
44
+ * @param listener Receives decoded Buffer chunks.
45
+ * @returns This decoder instance.
46
+ */
47
+ prependListener(event: 'data', listener: (data: Buffer) => void): this;
48
+
49
+ /**
50
+ * Emits a decoded flowed text chunk.
51
+ *
52
+ * @param event Event name.
53
+ * @param data Buffer chunk to emit.
54
+ * @returns `true` when the event had listeners.
55
+ */
56
+ emit(event: 'data', data: Buffer): boolean;
57
+ }
58
+
59
+ export = FlowedDecoder;
@@ -1,33 +1,46 @@
1
1
  'use strict';
2
2
 
3
- // Helper class to rewrite nodes with specific mime type
3
+ // Helper class to decode format=flowed text nodes
4
4
 
5
5
  const Transform = require('stream').Transform;
6
6
  const libmime = require('libmime');
7
7
 
8
+ /** @typedef {import('..').FlowedDecoderOptions} FlowedDecoderOptions */
9
+
10
+ const Libmime = /** @type {any} */ (libmime.Libmime);
11
+
8
12
  /**
9
- * Really bad "stream" transform to parse format=flowed content
13
+ * Transform stream that decodes text/plain format=flowed content.
10
14
  *
11
- * @constructor
12
- * @param {String} delSp True if delsp option was used
15
+ * @param {FlowedDecoderOptions} [config] Flowed text and charset decoding settings.
13
16
  */
14
17
  class FlowedDecoder extends Transform {
18
+ /**
19
+ * @param {FlowedDecoderOptions} [config] Flowed text and charset decoding settings.
20
+ */
15
21
  constructor(config) {
16
22
  super();
17
23
  this.config = config || {};
18
24
 
25
+ /** @type {Buffer[]} */
19
26
  this.chunks = [];
20
27
  this.chunklen = 0;
21
28
 
22
- this.libmime = new libmime.Libmime({ Iconv: config.Iconv });
29
+ this.libmime = new Libmime({ Iconv: this.config.Iconv });
23
30
  }
24
31
 
32
+ /**
33
+ * @param {Buffer | string} chunk
34
+ * @param {BufferEncoding} encoding
35
+ * @param {import('stream').TransformCallback} callback
36
+ * @returns {void}
37
+ */
25
38
  _transform(chunk, encoding, callback) {
26
39
  if (!chunk || !chunk.length) {
27
40
  return callback();
28
41
  }
29
42
 
30
- if (!encoding !== 'buffer') {
43
+ if (typeof chunk === 'string') {
31
44
  chunk = Buffer.from(chunk, encoding);
32
45
  }
33
46
 
@@ -37,6 +50,10 @@ class FlowedDecoder extends Transform {
37
50
  callback();
38
51
  }
39
52
 
53
+ /**
54
+ * @param {import('stream').TransformCallback} callback
55
+ * @returns {void}
56
+ */
40
57
  _flush(callback) {
41
58
  if (this.chunklen) {
42
59
  let currentBody = Buffer.concat(this.chunks, this.chunklen);
@@ -0,0 +1,117 @@
1
+ import type { DecodedHeader, HeaderLine, LibmimeOptions } from './types';
2
+
3
+ /** Mutable parser and builder for RFC-style message header blocks. */
4
+ declare class Headers {
5
+ /** Whether header lines have been modified after construction. */
6
+ changed: boolean;
7
+
8
+ /** Original unparsed header source, or `false` when constructed from parsed lines. */
9
+ headers: string | Buffer | false;
10
+
11
+ /** Whether `headers` has been parsed into `lines`. */
12
+ parsed: boolean;
13
+
14
+ /** Parsed header lines, or `false` until parsing occurs. */
15
+ lines: HeaderLine[] | false;
16
+
17
+ /** MBOX `From ` prefix line, or `false` when absent. */
18
+ mbox: string | false;
19
+
20
+ /** HTTP request prefix line, or `false` when absent. */
21
+ http: string | false;
22
+
23
+ /**
24
+ * Creates a mutable header collection.
25
+ *
26
+ * @param headers Raw header bytes/string, already parsed header lines, or `false` for an empty collection.
27
+ * @param config Optional libmime configuration.
28
+ */
29
+ constructor(headers?: string | Buffer | HeaderLine[] | false, config?: LibmimeOptions);
30
+
31
+ /**
32
+ * Checks whether at least one header with the requested key exists.
33
+ *
34
+ * @param key Header field name to find, case-insensitively.
35
+ * @returns `true` when the header exists.
36
+ */
37
+ hasHeader(key: string): boolean;
38
+
39
+ /**
40
+ * Gets all raw header lines for a key.
41
+ *
42
+ * @param key Header field name to find, case-insensitively.
43
+ * @returns Full decoded header lines, including field names.
44
+ */
45
+ get(key: string): string[];
46
+
47
+ /**
48
+ * Gets all decoded structured header values for a key.
49
+ *
50
+ * @param key Header field name to decode, case-insensitively.
51
+ * @returns Decoded header entries with key and value fields.
52
+ */
53
+ getDecoded(key: string): DecodedHeader[];
54
+
55
+ /**
56
+ * Gets the first decoded header value for a key.
57
+ *
58
+ * @param key Header field name to find, case-insensitively.
59
+ * @returns Trimmed decoded value, or an empty string when the header is absent.
60
+ */
61
+ getFirst(key: string): string;
62
+
63
+ /**
64
+ * Gets the mutable parsed header list.
65
+ *
66
+ * @returns Parsed header lines in message order.
67
+ */
68
+ getList(): HeaderLine[];
69
+
70
+ /**
71
+ * Adds a folded header line.
72
+ *
73
+ * @param key Header field name to add.
74
+ * @param value Header value; `undefined` leaves the collection unchanged.
75
+ * @param index Insertion index, where omitted or less than 1 inserts at the top.
76
+ * @returns Nothing.
77
+ */
78
+ add(key: string, value?: string | number | Buffer, index?: number): void;
79
+
80
+ /**
81
+ * Adds a preformatted header line.
82
+ *
83
+ * @param key Header field name used for normalized lookup.
84
+ * @param line Full header line to insert; falsy values leave the collection unchanged.
85
+ * @param index Insertion index, where omitted or less than 1 inserts at the top.
86
+ * @returns Nothing.
87
+ */
88
+ addFormatted(key: string, line?: string | Buffer | false, index?: number): void;
89
+
90
+ /**
91
+ * Removes all headers matching a key.
92
+ *
93
+ * @param key Header field name to remove, case-insensitively.
94
+ * @returns Nothing.
95
+ */
96
+ remove(key: string): void;
97
+
98
+ /**
99
+ * Replaces matching headers with a new folded header value.
100
+ *
101
+ * @param key Header field name to update.
102
+ * @param value Header value to write; `undefined` removes matching values without adding a replacement.
103
+ * @param relativeIndex Optional zero-based index among headers with the same key.
104
+ * @returns Nothing.
105
+ */
106
+ update(key: string, value?: string | number | Buffer, relativeIndex?: number): void;
107
+
108
+ /**
109
+ * Builds a raw header block.
110
+ *
111
+ * @param lineEnd Line ending to use when rebuilding changed headers; defaults to CRLF.
112
+ * @returns Header bytes ending with an empty header/body separator line.
113
+ */
114
+ build(lineEnd?: string | false): Buffer;
115
+ }
116
+
117
+ export = Headers;