@nocobase/plugin-notification-email 1.4.0-alpha

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.
Files changed (65) hide show
  1. package/LICENSE.txt +159 -0
  2. package/README.md +1 -0
  3. package/client.d.ts +2 -0
  4. package/client.js +1 -0
  5. package/dist/client/ConfigForm.d.ts +10 -0
  6. package/dist/client/MessageConfigForm.d.ts +12 -0
  7. package/dist/client/hooks/useTranslation.d.ts +9 -0
  8. package/dist/client/index.d.ts +15 -0
  9. package/dist/client/index.js +16 -0
  10. package/dist/constant.d.ts +10 -0
  11. package/dist/constant.js +39 -0
  12. package/dist/externalVersion.js +17 -0
  13. package/dist/index.d.ts +10 -0
  14. package/dist/index.js +48 -0
  15. package/dist/locale/en-US.json +22 -0
  16. package/dist/locale/zh-CN.json +22 -0
  17. package/dist/node_modules/nodemailer/.gitattributes +6 -0
  18. package/dist/node_modules/nodemailer/.ncurc.js +7 -0
  19. package/dist/node_modules/nodemailer/.prettierrc.js +8 -0
  20. package/dist/node_modules/nodemailer/LICENSE +16 -0
  21. package/dist/node_modules/nodemailer/SECURITY.txt +22 -0
  22. package/dist/node_modules/nodemailer/lib/addressparser/index.js +313 -0
  23. package/dist/node_modules/nodemailer/lib/base64/index.js +142 -0
  24. package/dist/node_modules/nodemailer/lib/dkim/index.js +251 -0
  25. package/dist/node_modules/nodemailer/lib/dkim/message-parser.js +155 -0
  26. package/dist/node_modules/nodemailer/lib/dkim/relaxed-body.js +154 -0
  27. package/dist/node_modules/nodemailer/lib/dkim/sign.js +117 -0
  28. package/dist/node_modules/nodemailer/lib/fetch/cookies.js +281 -0
  29. package/dist/node_modules/nodemailer/lib/fetch/index.js +274 -0
  30. package/dist/node_modules/nodemailer/lib/json-transport/index.js +82 -0
  31. package/dist/node_modules/nodemailer/lib/mail-composer/index.js +565 -0
  32. package/dist/node_modules/nodemailer/lib/mailer/index.js +427 -0
  33. package/dist/node_modules/nodemailer/lib/mailer/mail-message.js +315 -0
  34. package/dist/node_modules/nodemailer/lib/mime-funcs/index.js +625 -0
  35. package/dist/node_modules/nodemailer/lib/mime-funcs/mime-types.js +2102 -0
  36. package/dist/node_modules/nodemailer/lib/mime-node/index.js +1305 -0
  37. package/dist/node_modules/nodemailer/lib/mime-node/last-newline.js +33 -0
  38. package/dist/node_modules/nodemailer/lib/mime-node/le-unix.js +43 -0
  39. package/dist/node_modules/nodemailer/lib/mime-node/le-windows.js +52 -0
  40. package/dist/node_modules/nodemailer/lib/nodemailer.js +1 -0
  41. package/dist/node_modules/nodemailer/lib/qp/index.js +219 -0
  42. package/dist/node_modules/nodemailer/lib/sendmail-transport/index.js +210 -0
  43. package/dist/node_modules/nodemailer/lib/ses-transport/index.js +349 -0
  44. package/dist/node_modules/nodemailer/lib/shared/index.js +638 -0
  45. package/dist/node_modules/nodemailer/lib/smtp-connection/data-stream.js +108 -0
  46. package/dist/node_modules/nodemailer/lib/smtp-connection/http-proxy-client.js +143 -0
  47. package/dist/node_modules/nodemailer/lib/smtp-connection/index.js +1812 -0
  48. package/dist/node_modules/nodemailer/lib/smtp-pool/index.js +648 -0
  49. package/dist/node_modules/nodemailer/lib/smtp-pool/pool-resource.js +253 -0
  50. package/dist/node_modules/nodemailer/lib/smtp-transport/index.js +416 -0
  51. package/dist/node_modules/nodemailer/lib/stream-transport/index.js +135 -0
  52. package/dist/node_modules/nodemailer/lib/well-known/index.js +47 -0
  53. package/dist/node_modules/nodemailer/lib/well-known/services.json +338 -0
  54. package/dist/node_modules/nodemailer/lib/xoauth2/index.js +376 -0
  55. package/dist/node_modules/nodemailer/package.json +1 -0
  56. package/dist/server/index.d.ts +9 -0
  57. package/dist/server/index.js +42 -0
  58. package/dist/server/mail-server.d.ts +14 -0
  59. package/dist/server/mail-server.js +78 -0
  60. package/dist/server/plugin.d.ts +19 -0
  61. package/dist/server/plugin.js +69 -0
  62. package/package.json +23 -0
  63. package/server.d.ts +2 -0
  64. package/server.js +1 -0
  65. package/tsconfig.json +7 -0
@@ -0,0 +1,313 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Converts tokens for a single address into an address object
5
+ *
6
+ * @param {Array} tokens Tokens object
7
+ * @return {Object} Address object
8
+ */
9
+ function _handleAddress(tokens) {
10
+ let token;
11
+ let isGroup = false;
12
+ let state = 'text';
13
+ let address;
14
+ let addresses = [];
15
+ let data = {
16
+ address: [],
17
+ comment: [],
18
+ group: [],
19
+ text: []
20
+ };
21
+ let i;
22
+ let len;
23
+
24
+ // Filter out <addresses>, (comments) and regular text
25
+ for (i = 0, len = tokens.length; i < len; i++) {
26
+ token = tokens[i];
27
+ if (token.type === 'operator') {
28
+ switch (token.value) {
29
+ case '<':
30
+ state = 'address';
31
+ break;
32
+ case '(':
33
+ state = 'comment';
34
+ break;
35
+ case ':':
36
+ state = 'group';
37
+ isGroup = true;
38
+ break;
39
+ default:
40
+ state = 'text';
41
+ }
42
+ } else if (token.value) {
43
+ if (state === 'address') {
44
+ // handle use case where unquoted name includes a "<"
45
+ // Apple Mail truncates everything between an unexpected < and an address
46
+ // and so will we
47
+ token.value = token.value.replace(/^[^<]*<\s*/, '');
48
+ }
49
+ data[state].push(token.value);
50
+ }
51
+ }
52
+
53
+ // If there is no text but a comment, replace the two
54
+ if (!data.text.length && data.comment.length) {
55
+ data.text = data.comment;
56
+ data.comment = [];
57
+ }
58
+
59
+ if (isGroup) {
60
+ // http://tools.ietf.org/html/rfc2822#appendix-A.1.3
61
+ data.text = data.text.join(' ');
62
+ addresses.push({
63
+ name: data.text || (address && address.name),
64
+ group: data.group.length ? addressparser(data.group.join(',')) : []
65
+ });
66
+ } else {
67
+ // If no address was found, try to detect one from regular text
68
+ if (!data.address.length && data.text.length) {
69
+ for (i = data.text.length - 1; i >= 0; i--) {
70
+ if (data.text[i].match(/^[^@\s]+@[^@\s]+$/)) {
71
+ data.address = data.text.splice(i, 1);
72
+ break;
73
+ }
74
+ }
75
+
76
+ let _regexHandler = function (address) {
77
+ if (!data.address.length) {
78
+ data.address = [address.trim()];
79
+ return ' ';
80
+ } else {
81
+ return address;
82
+ }
83
+ };
84
+
85
+ // still no address
86
+ if (!data.address.length) {
87
+ for (i = data.text.length - 1; i >= 0; i--) {
88
+ // fixed the regex to parse email address correctly when email address has more than one @
89
+ data.text[i] = data.text[i].replace(/\s*\b[^@\s]+@[^\s]+\b\s*/, _regexHandler).trim();
90
+ if (data.address.length) {
91
+ break;
92
+ }
93
+ }
94
+ }
95
+ }
96
+
97
+ // If there's still is no text but a comment exixts, replace the two
98
+ if (!data.text.length && data.comment.length) {
99
+ data.text = data.comment;
100
+ data.comment = [];
101
+ }
102
+
103
+ // Keep only the first address occurence, push others to regular text
104
+ if (data.address.length > 1) {
105
+ data.text = data.text.concat(data.address.splice(1));
106
+ }
107
+
108
+ // Join values with spaces
109
+ data.text = data.text.join(' ');
110
+ data.address = data.address.join(' ');
111
+
112
+ if (!data.address && isGroup) {
113
+ return [];
114
+ } else {
115
+ address = {
116
+ address: data.address || data.text || '',
117
+ name: data.text || data.address || ''
118
+ };
119
+
120
+ if (address.address === address.name) {
121
+ if ((address.address || '').match(/@/)) {
122
+ address.name = '';
123
+ } else {
124
+ address.address = '';
125
+ }
126
+ }
127
+
128
+ addresses.push(address);
129
+ }
130
+ }
131
+
132
+ return addresses;
133
+ }
134
+
135
+ /**
136
+ * Creates a Tokenizer object for tokenizing address field strings
137
+ *
138
+ * @constructor
139
+ * @param {String} str Address field string
140
+ */
141
+ class Tokenizer {
142
+ constructor(str) {
143
+ this.str = (str || '').toString();
144
+ this.operatorCurrent = '';
145
+ this.operatorExpecting = '';
146
+ this.node = null;
147
+ this.escaped = false;
148
+
149
+ this.list = [];
150
+ /**
151
+ * Operator tokens and which tokens are expected to end the sequence
152
+ */
153
+ this.operators = {
154
+ '"': '"',
155
+ '(': ')',
156
+ '<': '>',
157
+ ',': '',
158
+ ':': ';',
159
+ // Semicolons are not a legal delimiter per the RFC2822 grammar other
160
+ // than for terminating a group, but they are also not valid for any
161
+ // other use in this context. Given that some mail clients have
162
+ // historically allowed the semicolon as a delimiter equivalent to the
163
+ // comma in their UI, it makes sense to treat them the same as a comma
164
+ // when used outside of a group.
165
+ ';': ''
166
+ };
167
+ }
168
+
169
+ /**
170
+ * Tokenizes the original input string
171
+ *
172
+ * @return {Array} An array of operator|text tokens
173
+ */
174
+ tokenize() {
175
+ let chr,
176
+ list = [];
177
+ for (let i = 0, len = this.str.length; i < len; i++) {
178
+ chr = this.str.charAt(i);
179
+ this.checkChar(chr);
180
+ }
181
+
182
+ this.list.forEach(node => {
183
+ node.value = (node.value || '').toString().trim();
184
+ if (node.value) {
185
+ list.push(node);
186
+ }
187
+ });
188
+
189
+ return list;
190
+ }
191
+
192
+ /**
193
+ * Checks if a character is an operator or text and acts accordingly
194
+ *
195
+ * @param {String} chr Character from the address field
196
+ */
197
+ checkChar(chr) {
198
+ if (this.escaped) {
199
+ // ignore next condition blocks
200
+ } else if (chr === this.operatorExpecting) {
201
+ this.node = {
202
+ type: 'operator',
203
+ value: chr
204
+ };
205
+ this.list.push(this.node);
206
+ this.node = null;
207
+ this.operatorExpecting = '';
208
+ this.escaped = false;
209
+ return;
210
+ } else if (!this.operatorExpecting && chr in this.operators) {
211
+ this.node = {
212
+ type: 'operator',
213
+ value: chr
214
+ };
215
+ this.list.push(this.node);
216
+ this.node = null;
217
+ this.operatorExpecting = this.operators[chr];
218
+ this.escaped = false;
219
+ return;
220
+ } else if (['"', "'"].includes(this.operatorExpecting) && chr === '\\') {
221
+ this.escaped = true;
222
+ return;
223
+ }
224
+
225
+ if (!this.node) {
226
+ this.node = {
227
+ type: 'text',
228
+ value: ''
229
+ };
230
+ this.list.push(this.node);
231
+ }
232
+
233
+ if (chr === '\n') {
234
+ // Convert newlines to spaces. Carriage return is ignored as \r and \n usually
235
+ // go together anyway and there already is a WS for \n. Lone \r means something is fishy.
236
+ chr = ' ';
237
+ }
238
+
239
+ if (chr.charCodeAt(0) >= 0x21 || [' ', '\t'].includes(chr)) {
240
+ // skip command bytes
241
+ this.node.value += chr;
242
+ }
243
+
244
+ this.escaped = false;
245
+ }
246
+ }
247
+
248
+ /**
249
+ * Parses structured e-mail addresses from an address field
250
+ *
251
+ * Example:
252
+ *
253
+ * 'Name <address@domain>'
254
+ *
255
+ * will be converted to
256
+ *
257
+ * [{name: 'Name', address: 'address@domain'}]
258
+ *
259
+ * @param {String} str Address field
260
+ * @return {Array} An array of address objects
261
+ */
262
+ function addressparser(str, options) {
263
+ options = options || {};
264
+
265
+ let tokenizer = new Tokenizer(str);
266
+ let tokens = tokenizer.tokenize();
267
+
268
+ let addresses = [];
269
+ let address = [];
270
+ let parsedAddresses = [];
271
+
272
+ tokens.forEach(token => {
273
+ if (token.type === 'operator' && (token.value === ',' || token.value === ';')) {
274
+ if (address.length) {
275
+ addresses.push(address);
276
+ }
277
+ address = [];
278
+ } else {
279
+ address.push(token);
280
+ }
281
+ });
282
+
283
+ if (address.length) {
284
+ addresses.push(address);
285
+ }
286
+
287
+ addresses.forEach(address => {
288
+ address = _handleAddress(address);
289
+ if (address.length) {
290
+ parsedAddresses = parsedAddresses.concat(address);
291
+ }
292
+ });
293
+
294
+ if (options.flatten) {
295
+ let addresses = [];
296
+ let walkAddressList = list => {
297
+ list.forEach(address => {
298
+ if (address.group) {
299
+ return walkAddressList(address.group);
300
+ } else {
301
+ addresses.push(address);
302
+ }
303
+ });
304
+ };
305
+ walkAddressList(parsedAddresses);
306
+ return addresses;
307
+ }
308
+
309
+ return parsedAddresses;
310
+ }
311
+
312
+ // expose to the world
313
+ module.exports = addressparser;
@@ -0,0 +1,142 @@
1
+ 'use strict';
2
+
3
+ const Transform = require('stream').Transform;
4
+
5
+ /**
6
+ * Encodes a Buffer into a base64 encoded string
7
+ *
8
+ * @param {Buffer} buffer Buffer to convert
9
+ * @returns {String} base64 encoded string
10
+ */
11
+ function encode(buffer) {
12
+ if (typeof buffer === 'string') {
13
+ buffer = Buffer.from(buffer, 'utf-8');
14
+ }
15
+
16
+ return buffer.toString('base64');
17
+ }
18
+
19
+ /**
20
+ * Adds soft line breaks to a base64 string
21
+ *
22
+ * @param {String} str base64 encoded string that might need line wrapping
23
+ * @param {Number} [lineLength=76] Maximum allowed length for a line
24
+ * @returns {String} Soft-wrapped base64 encoded string
25
+ */
26
+ function wrap(str, lineLength) {
27
+ str = (str || '').toString();
28
+ lineLength = lineLength || 76;
29
+
30
+ if (str.length <= lineLength) {
31
+ return str;
32
+ }
33
+
34
+ let result = [];
35
+ let pos = 0;
36
+ let chunkLength = lineLength * 1024;
37
+ while (pos < str.length) {
38
+ let wrappedLines = str
39
+ .substr(pos, chunkLength)
40
+ .replace(new RegExp('.{' + lineLength + '}', 'g'), '$&\r\n')
41
+ .trim();
42
+ result.push(wrappedLines);
43
+ pos += chunkLength;
44
+ }
45
+
46
+ return result.join('\r\n').trim();
47
+ }
48
+
49
+ /**
50
+ * Creates a transform stream for encoding data to base64 encoding
51
+ *
52
+ * @constructor
53
+ * @param {Object} options Stream options
54
+ * @param {Number} [options.lineLength=76] Maximum length for lines, set to false to disable wrapping
55
+ */
56
+ class Encoder extends Transform {
57
+ constructor(options) {
58
+ super();
59
+ // init Transform
60
+ this.options = options || {};
61
+
62
+ if (this.options.lineLength !== false) {
63
+ this.options.lineLength = this.options.lineLength || 76;
64
+ }
65
+
66
+ this._curLine = '';
67
+ this._remainingBytes = false;
68
+
69
+ this.inputBytes = 0;
70
+ this.outputBytes = 0;
71
+ }
72
+
73
+ _transform(chunk, encoding, done) {
74
+ if (encoding !== 'buffer') {
75
+ chunk = Buffer.from(chunk, encoding);
76
+ }
77
+
78
+ if (!chunk || !chunk.length) {
79
+ return setImmediate(done);
80
+ }
81
+
82
+ this.inputBytes += chunk.length;
83
+
84
+ if (this._remainingBytes && this._remainingBytes.length) {
85
+ chunk = Buffer.concat([this._remainingBytes, chunk], this._remainingBytes.length + chunk.length);
86
+ this._remainingBytes = false;
87
+ }
88
+
89
+ if (chunk.length % 3) {
90
+ this._remainingBytes = chunk.slice(chunk.length - (chunk.length % 3));
91
+ chunk = chunk.slice(0, chunk.length - (chunk.length % 3));
92
+ } else {
93
+ this._remainingBytes = false;
94
+ }
95
+
96
+ let b64 = this._curLine + encode(chunk);
97
+
98
+ if (this.options.lineLength) {
99
+ b64 = wrap(b64, this.options.lineLength);
100
+
101
+ // remove last line as it is still most probably incomplete
102
+ let lastLF = b64.lastIndexOf('\n');
103
+ if (lastLF < 0) {
104
+ this._curLine = b64;
105
+ b64 = '';
106
+ } else if (lastLF === b64.length - 1) {
107
+ this._curLine = '';
108
+ } else {
109
+ this._curLine = b64.substr(lastLF + 1);
110
+ b64 = b64.substr(0, lastLF + 1);
111
+ }
112
+ }
113
+
114
+ if (b64) {
115
+ this.outputBytes += b64.length;
116
+ this.push(Buffer.from(b64, 'ascii'));
117
+ }
118
+
119
+ setImmediate(done);
120
+ }
121
+
122
+ _flush(done) {
123
+ if (this._remainingBytes && this._remainingBytes.length) {
124
+ this._curLine += encode(this._remainingBytes);
125
+ }
126
+
127
+ if (this._curLine) {
128
+ this._curLine = wrap(this._curLine, this.options.lineLength);
129
+ this.outputBytes += this._curLine.length;
130
+ this.push(this._curLine, 'ascii');
131
+ this._curLine = '';
132
+ }
133
+ done();
134
+ }
135
+ }
136
+
137
+ // expose to the world
138
+ module.exports = {
139
+ encode,
140
+ wrap,
141
+ Encoder
142
+ };
@@ -0,0 +1,251 @@
1
+ 'use strict';
2
+
3
+ // FIXME:
4
+ // replace this Transform mess with a method that pipes input argument to output argument
5
+
6
+ const MessageParser = require('./message-parser');
7
+ const RelaxedBody = require('./relaxed-body');
8
+ const sign = require('./sign');
9
+ const PassThrough = require('stream').PassThrough;
10
+ const fs = require('fs');
11
+ const path = require('path');
12
+ const crypto = require('crypto');
13
+
14
+ const DKIM_ALGO = 'sha256';
15
+ const MAX_MESSAGE_SIZE = 128 * 1024; // buffer messages larger than this to disk
16
+
17
+ /*
18
+ // Usage:
19
+
20
+ let dkim = new DKIM({
21
+ domainName: 'example.com',
22
+ keySelector: 'key-selector',
23
+ privateKey,
24
+ cacheDir: '/tmp'
25
+ });
26
+ dkim.sign(input).pipe(process.stdout);
27
+
28
+ // Where inputStream is a rfc822 message (either a stream, string or Buffer)
29
+ // and outputStream is a DKIM signed rfc822 message
30
+ */
31
+
32
+ class DKIMSigner {
33
+ constructor(options, keys, input, output) {
34
+ this.options = options || {};
35
+ this.keys = keys;
36
+
37
+ this.cacheTreshold = Number(this.options.cacheTreshold) || MAX_MESSAGE_SIZE;
38
+ this.hashAlgo = this.options.hashAlgo || DKIM_ALGO;
39
+
40
+ this.cacheDir = this.options.cacheDir || false;
41
+
42
+ this.chunks = [];
43
+ this.chunklen = 0;
44
+ this.readPos = 0;
45
+ this.cachePath = this.cacheDir ? path.join(this.cacheDir, 'message.' + Date.now() + '-' + crypto.randomBytes(14).toString('hex')) : false;
46
+ this.cache = false;
47
+
48
+ this.headers = false;
49
+ this.bodyHash = false;
50
+ this.parser = false;
51
+ this.relaxedBody = false;
52
+
53
+ this.input = input;
54
+ this.output = output;
55
+ this.output.usingCache = false;
56
+
57
+ this.hasErrored = false;
58
+
59
+ this.input.on('error', err => {
60
+ this.hasErrored = true;
61
+ this.cleanup();
62
+ output.emit('error', err);
63
+ });
64
+ }
65
+
66
+ cleanup() {
67
+ if (!this.cache || !this.cachePath) {
68
+ return;
69
+ }
70
+ fs.unlink(this.cachePath, () => false);
71
+ }
72
+
73
+ createReadCache() {
74
+ // pipe remainings to cache file
75
+ this.cache = fs.createReadStream(this.cachePath);
76
+ this.cache.once('error', err => {
77
+ this.cleanup();
78
+ this.output.emit('error', err);
79
+ });
80
+ this.cache.once('close', () => {
81
+ this.cleanup();
82
+ });
83
+ this.cache.pipe(this.output);
84
+ }
85
+
86
+ sendNextChunk() {
87
+ if (this.hasErrored) {
88
+ return;
89
+ }
90
+
91
+ if (this.readPos >= this.chunks.length) {
92
+ if (!this.cache) {
93
+ return this.output.end();
94
+ }
95
+ return this.createReadCache();
96
+ }
97
+ let chunk = this.chunks[this.readPos++];
98
+ if (this.output.write(chunk) === false) {
99
+ return this.output.once('drain', () => {
100
+ this.sendNextChunk();
101
+ });
102
+ }
103
+ setImmediate(() => this.sendNextChunk());
104
+ }
105
+
106
+ sendSignedOutput() {
107
+ let keyPos = 0;
108
+ let signNextKey = () => {
109
+ if (keyPos >= this.keys.length) {
110
+ this.output.write(this.parser.rawHeaders);
111
+ return setImmediate(() => this.sendNextChunk());
112
+ }
113
+ let key = this.keys[keyPos++];
114
+ let dkimField = sign(this.headers, this.hashAlgo, this.bodyHash, {
115
+ domainName: key.domainName,
116
+ keySelector: key.keySelector,
117
+ privateKey: key.privateKey,
118
+ headerFieldNames: this.options.headerFieldNames,
119
+ skipFields: this.options.skipFields
120
+ });
121
+ if (dkimField) {
122
+ this.output.write(Buffer.from(dkimField + '\r\n'));
123
+ }
124
+ return setImmediate(signNextKey);
125
+ };
126
+
127
+ if (this.bodyHash && this.headers) {
128
+ return signNextKey();
129
+ }
130
+
131
+ this.output.write(this.parser.rawHeaders);
132
+ this.sendNextChunk();
133
+ }
134
+
135
+ createWriteCache() {
136
+ this.output.usingCache = true;
137
+ // pipe remainings to cache file
138
+ this.cache = fs.createWriteStream(this.cachePath);
139
+ this.cache.once('error', err => {
140
+ this.cleanup();
141
+ // drain input
142
+ this.relaxedBody.unpipe(this.cache);
143
+ this.relaxedBody.on('readable', () => {
144
+ while (this.relaxedBody.read() !== null) {
145
+ // do nothing
146
+ }
147
+ });
148
+ this.hasErrored = true;
149
+ // emit error
150
+ this.output.emit('error', err);
151
+ });
152
+ this.cache.once('close', () => {
153
+ this.sendSignedOutput();
154
+ });
155
+ this.relaxedBody.removeAllListeners('readable');
156
+ this.relaxedBody.pipe(this.cache);
157
+ }
158
+
159
+ signStream() {
160
+ this.parser = new MessageParser();
161
+ this.relaxedBody = new RelaxedBody({
162
+ hashAlgo: this.hashAlgo
163
+ });
164
+
165
+ this.parser.on('headers', value => {
166
+ this.headers = value;
167
+ });
168
+
169
+ this.relaxedBody.on('hash', value => {
170
+ this.bodyHash = value;
171
+ });
172
+
173
+ this.relaxedBody.on('readable', () => {
174
+ let chunk;
175
+ if (this.cache) {
176
+ return;
177
+ }
178
+ while ((chunk = this.relaxedBody.read()) !== null) {
179
+ this.chunks.push(chunk);
180
+ this.chunklen += chunk.length;
181
+ if (this.chunklen >= this.cacheTreshold && this.cachePath) {
182
+ return this.createWriteCache();
183
+ }
184
+ }
185
+ });
186
+
187
+ this.relaxedBody.on('end', () => {
188
+ if (this.cache) {
189
+ return;
190
+ }
191
+ this.sendSignedOutput();
192
+ });
193
+
194
+ this.parser.pipe(this.relaxedBody);
195
+ setImmediate(() => this.input.pipe(this.parser));
196
+ }
197
+ }
198
+
199
+ class DKIM {
200
+ constructor(options) {
201
+ this.options = options || {};
202
+ this.keys = [].concat(
203
+ this.options.keys || {
204
+ domainName: options.domainName,
205
+ keySelector: options.keySelector,
206
+ privateKey: options.privateKey
207
+ }
208
+ );
209
+ }
210
+
211
+ sign(input, extraOptions) {
212
+ let output = new PassThrough();
213
+ let inputStream = input;
214
+ let writeValue = false;
215
+
216
+ if (Buffer.isBuffer(input)) {
217
+ writeValue = input;
218
+ inputStream = new PassThrough();
219
+ } else if (typeof input === 'string') {
220
+ writeValue = Buffer.from(input);
221
+ inputStream = new PassThrough();
222
+ }
223
+
224
+ let options = this.options;
225
+ if (extraOptions && Object.keys(extraOptions).length) {
226
+ options = {};
227
+ Object.keys(this.options || {}).forEach(key => {
228
+ options[key] = this.options[key];
229
+ });
230
+ Object.keys(extraOptions || {}).forEach(key => {
231
+ if (!(key in options)) {
232
+ options[key] = extraOptions[key];
233
+ }
234
+ });
235
+ }
236
+
237
+ let signer = new DKIMSigner(options, this.keys, inputStream, output);
238
+ setImmediate(() => {
239
+ signer.signStream();
240
+ if (writeValue) {
241
+ setImmediate(() => {
242
+ inputStream.end(writeValue);
243
+ });
244
+ }
245
+ });
246
+
247
+ return output;
248
+ }
249
+ }
250
+
251
+ module.exports = DKIM;