m06_task3.js 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. package/.idea/.name +1 -0
  2. package/.idea/inspectionProfiles/Project_Default.xml +10 -0
  3. package/.idea/jsLibraryMappings.xml +6 -0
  4. package/.idea/modules.xml +8 -0
  5. package/.idea/node_lab_6.iml +12 -0
  6. package/06_task2.html +73 -0
  7. package/06_task2.js +52 -0
  8. package/06_task3.js +3 -0
  9. package/06_task4.js +3 -0
  10. package/m06_task3.js +28 -0
  11. package/package/index.js +28 -0
  12. package/package/node_modules/.package-lock.json +31 -0
  13. package/package/node_modules/@types/node/LICENSE +21 -0
  14. package/package/node_modules/@types/node/README.md +16 -0
  15. package/package/node_modules/@types/node/assert/strict.d.ts +8 -0
  16. package/package/node_modules/@types/node/assert.d.ts +961 -0
  17. package/package/node_modules/@types/node/async_hooks.d.ts +501 -0
  18. package/package/node_modules/@types/node/buffer.d.ts +2258 -0
  19. package/package/node_modules/@types/node/child_process.d.ts +1369 -0
  20. package/package/node_modules/@types/node/cluster.d.ts +410 -0
  21. package/package/node_modules/@types/node/console.d.ts +412 -0
  22. package/package/node_modules/@types/node/constants.d.ts +18 -0
  23. package/package/node_modules/@types/node/crypto.d.ts +3964 -0
  24. package/package/node_modules/@types/node/dgram.d.ts +545 -0
  25. package/package/node_modules/@types/node/diagnostics_channel.d.ts +153 -0
  26. package/package/node_modules/@types/node/dns/promises.d.ts +370 -0
  27. package/package/node_modules/@types/node/dns.d.ts +659 -0
  28. package/package/node_modules/@types/node/dom-events.d.ts +126 -0
  29. package/package/node_modules/@types/node/domain.d.ts +170 -0
  30. package/package/node_modules/@types/node/events.d.ts +678 -0
  31. package/package/node_modules/@types/node/fs/promises.d.ts +1138 -0
  32. package/package/node_modules/@types/node/fs.d.ts +3872 -0
  33. package/package/node_modules/@types/node/globals.d.ts +300 -0
  34. package/package/node_modules/@types/node/globals.global.d.ts +1 -0
  35. package/package/node_modules/@types/node/http.d.ts +1614 -0
  36. package/package/node_modules/@types/node/http2.d.ts +2134 -0
  37. package/package/node_modules/@types/node/https.d.ts +541 -0
  38. package/package/node_modules/@types/node/index.d.ts +133 -0
  39. package/package/node_modules/@types/node/inspector.d.ts +2741 -0
  40. package/package/node_modules/@types/node/module.d.ts +114 -0
  41. package/package/node_modules/@types/node/net.d.ts +869 -0
  42. package/package/node_modules/@types/node/os.d.ts +466 -0
  43. package/package/node_modules/@types/node/package.json +232 -0
  44. package/package/node_modules/@types/node/path.d.ts +191 -0
  45. package/package/node_modules/@types/node/perf_hooks.d.ts +625 -0
  46. package/package/node_modules/@types/node/process.d.ts +1482 -0
  47. package/package/node_modules/@types/node/punycode.d.ts +117 -0
  48. package/package/node_modules/@types/node/querystring.d.ts +131 -0
  49. package/package/node_modules/@types/node/readline/promises.d.ts +143 -0
  50. package/package/node_modules/@types/node/readline.d.ts +653 -0
  51. package/package/node_modules/@types/node/repl.d.ts +424 -0
  52. package/package/node_modules/@types/node/stream/consumers.d.ts +12 -0
  53. package/package/node_modules/@types/node/stream/promises.d.ts +42 -0
  54. package/package/node_modules/@types/node/stream/web.d.ts +330 -0
  55. package/package/node_modules/@types/node/stream.d.ts +1340 -0
  56. package/package/node_modules/@types/node/string_decoder.d.ts +67 -0
  57. package/package/node_modules/@types/node/test.d.ts +314 -0
  58. package/package/node_modules/@types/node/timers/promises.d.ts +68 -0
  59. package/package/node_modules/@types/node/timers.d.ts +94 -0
  60. package/package/node_modules/@types/node/tls.d.ts +1028 -0
  61. package/package/node_modules/@types/node/trace_events.d.ts +171 -0
  62. package/package/node_modules/@types/node/ts4.8/assert/strict.d.ts +8 -0
  63. package/package/node_modules/@types/node/ts4.8/assert.d.ts +961 -0
  64. package/package/node_modules/@types/node/ts4.8/async_hooks.d.ts +501 -0
  65. package/package/node_modules/@types/node/ts4.8/buffer.d.ts +2259 -0
  66. package/package/node_modules/@types/node/ts4.8/child_process.d.ts +1369 -0
  67. package/package/node_modules/@types/node/ts4.8/cluster.d.ts +410 -0
  68. package/package/node_modules/@types/node/ts4.8/console.d.ts +412 -0
  69. package/package/node_modules/@types/node/ts4.8/constants.d.ts +18 -0
  70. package/package/node_modules/@types/node/ts4.8/crypto.d.ts +3964 -0
  71. package/package/node_modules/@types/node/ts4.8/dgram.d.ts +545 -0
  72. package/package/node_modules/@types/node/ts4.8/diagnostics_channel.d.ts +153 -0
  73. package/package/node_modules/@types/node/ts4.8/dns/promises.d.ts +370 -0
  74. package/package/node_modules/@types/node/ts4.8/dns.d.ts +659 -0
  75. package/package/node_modules/@types/node/ts4.8/dom-events.d.ts +126 -0
  76. package/package/node_modules/@types/node/ts4.8/domain.d.ts +170 -0
  77. package/package/node_modules/@types/node/ts4.8/events.d.ts +678 -0
  78. package/package/node_modules/@types/node/ts4.8/fs/promises.d.ts +1138 -0
  79. package/package/node_modules/@types/node/ts4.8/fs.d.ts +3872 -0
  80. package/package/node_modules/@types/node/ts4.8/globals.d.ts +294 -0
  81. package/package/node_modules/@types/node/ts4.8/globals.global.d.ts +1 -0
  82. package/package/node_modules/@types/node/ts4.8/http.d.ts +1614 -0
  83. package/package/node_modules/@types/node/ts4.8/http2.d.ts +2134 -0
  84. package/package/node_modules/@types/node/ts4.8/https.d.ts +541 -0
  85. package/package/node_modules/@types/node/ts4.8/index.d.ts +88 -0
  86. package/package/node_modules/@types/node/ts4.8/inspector.d.ts +2741 -0
  87. package/package/node_modules/@types/node/ts4.8/module.d.ts +114 -0
  88. package/package/node_modules/@types/node/ts4.8/net.d.ts +869 -0
  89. package/package/node_modules/@types/node/ts4.8/os.d.ts +466 -0
  90. package/package/node_modules/@types/node/ts4.8/path.d.ts +191 -0
  91. package/package/node_modules/@types/node/ts4.8/perf_hooks.d.ts +625 -0
  92. package/package/node_modules/@types/node/ts4.8/process.d.ts +1482 -0
  93. package/package/node_modules/@types/node/ts4.8/punycode.d.ts +117 -0
  94. package/package/node_modules/@types/node/ts4.8/querystring.d.ts +131 -0
  95. package/package/node_modules/@types/node/ts4.8/readline/promises.d.ts +143 -0
  96. package/package/node_modules/@types/node/ts4.8/readline.d.ts +653 -0
  97. package/package/node_modules/@types/node/ts4.8/repl.d.ts +424 -0
  98. package/package/node_modules/@types/node/ts4.8/stream/consumers.d.ts +12 -0
  99. package/package/node_modules/@types/node/ts4.8/stream/promises.d.ts +42 -0
  100. package/package/node_modules/@types/node/ts4.8/stream/web.d.ts +330 -0
  101. package/package/node_modules/@types/node/ts4.8/stream.d.ts +1340 -0
  102. package/package/node_modules/@types/node/ts4.8/string_decoder.d.ts +67 -0
  103. package/package/node_modules/@types/node/ts4.8/test.d.ts +314 -0
  104. package/package/node_modules/@types/node/ts4.8/timers/promises.d.ts +68 -0
  105. package/package/node_modules/@types/node/ts4.8/timers.d.ts +94 -0
  106. package/package/node_modules/@types/node/ts4.8/tls.d.ts +1028 -0
  107. package/package/node_modules/@types/node/ts4.8/trace_events.d.ts +171 -0
  108. package/package/node_modules/@types/node/ts4.8/tty.d.ts +206 -0
  109. package/package/node_modules/@types/node/ts4.8/url.d.ts +897 -0
  110. package/package/node_modules/@types/node/ts4.8/util.d.ts +1850 -0
  111. package/package/node_modules/@types/node/ts4.8/v8.d.ts +396 -0
  112. package/package/node_modules/@types/node/ts4.8/vm.d.ts +509 -0
  113. package/package/node_modules/@types/node/ts4.8/wasi.d.ts +158 -0
  114. package/package/node_modules/@types/node/ts4.8/worker_threads.d.ts +689 -0
  115. package/package/node_modules/@types/node/ts4.8/zlib.d.ts +517 -0
  116. package/package/node_modules/@types/node/tty.d.ts +206 -0
  117. package/package/node_modules/@types/node/url.d.ts +897 -0
  118. package/package/node_modules/@types/node/util.d.ts +1850 -0
  119. package/package/node_modules/@types/node/v8.d.ts +396 -0
  120. package/package/node_modules/@types/node/vm.d.ts +509 -0
  121. package/package/node_modules/@types/node/wasi.d.ts +158 -0
  122. package/package/node_modules/@types/node/worker_threads.d.ts +689 -0
  123. package/package/node_modules/@types/node/zlib.d.ts +517 -0
  124. package/package/node_modules/@types/nodemailer/LICENSE +21 -0
  125. package/package/node_modules/@types/nodemailer/README.md +16 -0
  126. package/package/node_modules/@types/nodemailer/index.d.ts +83 -0
  127. package/package/node_modules/@types/nodemailer/lib/addressparser/index.d.ts +31 -0
  128. package/package/node_modules/@types/nodemailer/lib/base64/index.d.ts +22 -0
  129. package/package/node_modules/@types/nodemailer/lib/dkim/index.d.ts +45 -0
  130. package/package/node_modules/@types/nodemailer/lib/dkim/message-parser.d.ts +75 -0
  131. package/package/node_modules/@types/nodemailer/lib/dkim/relaxed-body.d.ts +75 -0
  132. package/package/node_modules/@types/nodemailer/lib/dkim/sign.d.ts +11 -0
  133. package/package/node_modules/@types/nodemailer/lib/fetch/cookies.d.ts +54 -0
  134. package/package/node_modules/@types/nodemailer/lib/fetch/index.d.ts +38 -0
  135. package/package/node_modules/@types/nodemailer/lib/json-transport/index.d.ts +50 -0
  136. package/package/node_modules/@types/nodemailer/lib/mail-composer/index.d.ts +25 -0
  137. package/package/node_modules/@types/nodemailer/lib/mailer/index.d.ts +230 -0
  138. package/package/node_modules/@types/nodemailer/lib/mailer/mail-message.d.ts +28 -0
  139. package/package/node_modules/@types/nodemailer/lib/mime-funcs/index.d.ts +87 -0
  140. package/package/node_modules/@types/nodemailer/lib/mime-funcs/mime-types.d.ts +2 -0
  141. package/package/node_modules/@types/nodemailer/lib/mime-node/index.d.ts +137 -0
  142. package/package/node_modules/@types/nodemailer/lib/mime-node/last-newline.d.ts +9 -0
  143. package/package/node_modules/@types/nodemailer/lib/qp/index.d.ts +23 -0
  144. package/package/node_modules/@types/nodemailer/lib/sendmail-transport/index.d.ts +53 -0
  145. package/package/node_modules/@types/nodemailer/lib/sendmail-transport/le-unix.d.ts +7 -0
  146. package/package/node_modules/@types/nodemailer/lib/sendmail-transport/le-windows.d.ts +7 -0
  147. package/package/node_modules/@types/nodemailer/lib/ses-transport/index.d.ts +136 -0
  148. package/package/node_modules/@types/nodemailer/lib/shared/index.d.ts +51 -0
  149. package/package/node_modules/@types/nodemailer/lib/smtp-connection/data-stream.d.ts +11 -0
  150. package/package/node_modules/@types/nodemailer/lib/smtp-connection/http-proxy-client.d.ts +11 -0
  151. package/package/node_modules/@types/nodemailer/lib/smtp-connection/index.d.ts +254 -0
  152. package/package/node_modules/@types/nodemailer/lib/smtp-pool/index.d.ts +90 -0
  153. package/package/node_modules/@types/nodemailer/lib/smtp-pool/pool-resource.d.ts +66 -0
  154. package/package/node_modules/@types/nodemailer/lib/smtp-transport/index.d.ts +115 -0
  155. package/package/node_modules/@types/nodemailer/lib/stream-transport/index.d.ts +56 -0
  156. package/package/node_modules/@types/nodemailer/lib/well-known/index.d.ts +6 -0
  157. package/package/node_modules/@types/nodemailer/lib/xoauth2/index.d.ts +110 -0
  158. package/package/node_modules/@types/nodemailer/package.json +37 -0
  159. package/package/node_modules/nodemailer/.gitattributes +6 -0
  160. package/package/node_modules/nodemailer/.prettierrc.js +8 -0
  161. package/package/node_modules/nodemailer/CHANGELOG.md +715 -0
  162. package/package/node_modules/nodemailer/CODE_OF_CONDUCT.md +76 -0
  163. package/package/node_modules/nodemailer/CONTRIBUTING.md +67 -0
  164. package/package/node_modules/nodemailer/LICENSE +16 -0
  165. package/package/node_modules/nodemailer/README.md +93 -0
  166. package/package/node_modules/nodemailer/SECURITY.txt +22 -0
  167. package/package/node_modules/nodemailer/lib/addressparser/index.js +313 -0
  168. package/package/node_modules/nodemailer/lib/base64/index.js +142 -0
  169. package/package/node_modules/nodemailer/lib/dkim/index.js +251 -0
  170. package/package/node_modules/nodemailer/lib/dkim/message-parser.js +155 -0
  171. package/package/node_modules/nodemailer/lib/dkim/relaxed-body.js +154 -0
  172. package/package/node_modules/nodemailer/lib/dkim/sign.js +117 -0
  173. package/package/node_modules/nodemailer/lib/fetch/cookies.js +281 -0
  174. package/package/node_modules/nodemailer/lib/fetch/index.js +269 -0
  175. package/package/node_modules/nodemailer/lib/json-transport/index.js +82 -0
  176. package/package/node_modules/nodemailer/lib/mail-composer/index.js +558 -0
  177. package/package/node_modules/nodemailer/lib/mailer/index.js +427 -0
  178. package/package/node_modules/nodemailer/lib/mailer/mail-message.js +315 -0
  179. package/package/node_modules/nodemailer/lib/mime-funcs/index.js +619 -0
  180. package/package/node_modules/nodemailer/lib/mime-funcs/mime-types.js +2102 -0
  181. package/package/node_modules/nodemailer/lib/mime-node/index.js +1290 -0
  182. package/package/node_modules/nodemailer/lib/mime-node/last-newline.js +33 -0
  183. package/package/node_modules/nodemailer/lib/mime-node/le-unix.js +43 -0
  184. package/package/node_modules/nodemailer/lib/mime-node/le-windows.js +52 -0
  185. package/package/node_modules/nodemailer/lib/nodemailer.js +143 -0
  186. package/package/node_modules/nodemailer/lib/qp/index.js +219 -0
  187. package/package/node_modules/nodemailer/lib/sendmail-transport/index.js +210 -0
  188. package/package/node_modules/nodemailer/lib/ses-transport/index.js +349 -0
  189. package/package/node_modules/nodemailer/lib/shared/index.js +639 -0
  190. package/package/node_modules/nodemailer/lib/smtp-connection/data-stream.js +108 -0
  191. package/package/node_modules/nodemailer/lib/smtp-connection/http-proxy-client.js +143 -0
  192. package/package/node_modules/nodemailer/lib/smtp-connection/index.js +1786 -0
  193. package/package/node_modules/nodemailer/lib/smtp-pool/index.js +648 -0
  194. package/package/node_modules/nodemailer/lib/smtp-pool/pool-resource.js +253 -0
  195. package/package/node_modules/nodemailer/lib/smtp-transport/index.js +416 -0
  196. package/package/node_modules/nodemailer/lib/stream-transport/index.js +135 -0
  197. package/package/node_modules/nodemailer/lib/well-known/index.js +47 -0
  198. package/package/node_modules/nodemailer/lib/well-known/services.json +286 -0
  199. package/package/node_modules/nodemailer/lib/xoauth2/index.js +376 -0
  200. package/package/node_modules/nodemailer/package.json +46 -0
  201. package/package/node_modules/nodemailer/postinstall.js +101 -0
  202. package/package/package-lock.json +60 -0
  203. package/package/package.json +6 -0
  204. package/package.json +19 -0
@@ -0,0 +1,648 @@
1
+ 'use strict';
2
+
3
+ const EventEmitter = require('events');
4
+ const PoolResource = require('./pool-resource');
5
+ const SMTPConnection = require('../smtp-connection');
6
+ const wellKnown = require('../well-known');
7
+ const shared = require('../shared');
8
+ const packageData = require('../../package.json');
9
+
10
+ /**
11
+ * Creates a SMTP pool transport object for Nodemailer
12
+ *
13
+ * @constructor
14
+ * @param {Object} options SMTP Connection options
15
+ */
16
+ class SMTPPool extends EventEmitter {
17
+ constructor(options) {
18
+ super();
19
+
20
+ options = options || {};
21
+ if (typeof options === 'string') {
22
+ options = {
23
+ url: options
24
+ };
25
+ }
26
+
27
+ let urlData;
28
+ let service = options.service;
29
+
30
+ if (typeof options.getSocket === 'function') {
31
+ this.getSocket = options.getSocket;
32
+ }
33
+
34
+ if (options.url) {
35
+ urlData = shared.parseConnectionUrl(options.url);
36
+ service = service || urlData.service;
37
+ }
38
+
39
+ this.options = shared.assign(
40
+ false, // create new object
41
+ options, // regular options
42
+ urlData, // url options
43
+ service && wellKnown(service) // wellknown options
44
+ );
45
+
46
+ this.options.maxConnections = this.options.maxConnections || 5;
47
+ this.options.maxMessages = this.options.maxMessages || 100;
48
+
49
+ this.logger = shared.getLogger(this.options, {
50
+ component: this.options.component || 'smtp-pool'
51
+ });
52
+
53
+ // temporary object
54
+ let connection = new SMTPConnection(this.options);
55
+
56
+ this.name = 'SMTP (pool)';
57
+ this.version = packageData.version + '[client:' + connection.version + ']';
58
+
59
+ this._rateLimit = {
60
+ counter: 0,
61
+ timeout: null,
62
+ waiting: [],
63
+ checkpoint: false,
64
+ delta: Number(this.options.rateDelta) || 1000,
65
+ limit: Number(this.options.rateLimit) || 0
66
+ };
67
+ this._closed = false;
68
+ this._queue = [];
69
+ this._connections = [];
70
+ this._connectionCounter = 0;
71
+
72
+ this.idling = true;
73
+
74
+ setImmediate(() => {
75
+ if (this.idling) {
76
+ this.emit('idle');
77
+ }
78
+ });
79
+ }
80
+
81
+ /**
82
+ * Placeholder function for creating proxy sockets. This method immediatelly returns
83
+ * without a socket
84
+ *
85
+ * @param {Object} options Connection options
86
+ * @param {Function} callback Callback function to run with the socket keys
87
+ */
88
+ getSocket(options, callback) {
89
+ // return immediatelly
90
+ return setImmediate(() => callback(null, false));
91
+ }
92
+
93
+ /**
94
+ * Queues an e-mail to be sent using the selected settings
95
+ *
96
+ * @param {Object} mail Mail object
97
+ * @param {Function} callback Callback function
98
+ */
99
+ send(mail, callback) {
100
+ if (this._closed) {
101
+ return false;
102
+ }
103
+
104
+ this._queue.push({
105
+ mail,
106
+ requeueAttempts: 0,
107
+ callback
108
+ });
109
+
110
+ if (this.idling && this._queue.length >= this.options.maxConnections) {
111
+ this.idling = false;
112
+ }
113
+
114
+ setImmediate(() => this._processMessages());
115
+
116
+ return true;
117
+ }
118
+
119
+ /**
120
+ * Closes all connections in the pool. If there is a message being sent, the connection
121
+ * is closed later
122
+ */
123
+ close() {
124
+ let connection;
125
+ let len = this._connections.length;
126
+ this._closed = true;
127
+
128
+ // clear rate limit timer if it exists
129
+ clearTimeout(this._rateLimit.timeout);
130
+
131
+ if (!len && !this._queue.length) {
132
+ return;
133
+ }
134
+
135
+ // remove all available connections
136
+ for (let i = len - 1; i >= 0; i--) {
137
+ if (this._connections[i] && this._connections[i].available) {
138
+ connection = this._connections[i];
139
+ connection.close();
140
+ this.logger.info(
141
+ {
142
+ tnx: 'connection',
143
+ cid: connection.id,
144
+ action: 'removed'
145
+ },
146
+ 'Connection #%s removed',
147
+ connection.id
148
+ );
149
+ }
150
+ }
151
+
152
+ if (len && !this._connections.length) {
153
+ this.logger.debug(
154
+ {
155
+ tnx: 'connection'
156
+ },
157
+ 'All connections removed'
158
+ );
159
+ }
160
+
161
+ if (!this._queue.length) {
162
+ return;
163
+ }
164
+
165
+ // make sure that entire queue would be cleaned
166
+ let invokeCallbacks = () => {
167
+ if (!this._queue.length) {
168
+ this.logger.debug(
169
+ {
170
+ tnx: 'connection'
171
+ },
172
+ 'Pending queue entries cleared'
173
+ );
174
+ return;
175
+ }
176
+ let entry = this._queue.shift();
177
+ if (entry && typeof entry.callback === 'function') {
178
+ try {
179
+ entry.callback(new Error('Connection pool was closed'));
180
+ } catch (E) {
181
+ this.logger.error(
182
+ {
183
+ err: E,
184
+ tnx: 'callback',
185
+ cid: connection.id
186
+ },
187
+ 'Callback error for #%s: %s',
188
+ connection.id,
189
+ E.message
190
+ );
191
+ }
192
+ }
193
+ setImmediate(invokeCallbacks);
194
+ };
195
+ setImmediate(invokeCallbacks);
196
+ }
197
+
198
+ /**
199
+ * Check the queue and available connections. If there is a message to be sent and there is
200
+ * an available connection, then use this connection to send the mail
201
+ */
202
+ _processMessages() {
203
+ let connection;
204
+ let i, len;
205
+
206
+ // do nothing if already closed
207
+ if (this._closed) {
208
+ return;
209
+ }
210
+
211
+ // do nothing if queue is empty
212
+ if (!this._queue.length) {
213
+ if (!this.idling) {
214
+ // no pending jobs
215
+ this.idling = true;
216
+ this.emit('idle');
217
+ }
218
+ return;
219
+ }
220
+
221
+ // find first available connection
222
+ for (i = 0, len = this._connections.length; i < len; i++) {
223
+ if (this._connections[i].available) {
224
+ connection = this._connections[i];
225
+ break;
226
+ }
227
+ }
228
+
229
+ if (!connection && this._connections.length < this.options.maxConnections) {
230
+ connection = this._createConnection();
231
+ }
232
+
233
+ if (!connection) {
234
+ // no more free connection slots available
235
+ this.idling = false;
236
+ return;
237
+ }
238
+
239
+ // check if there is free space in the processing queue
240
+ if (!this.idling && this._queue.length < this.options.maxConnections) {
241
+ this.idling = true;
242
+ this.emit('idle');
243
+ }
244
+
245
+ let entry = (connection.queueEntry = this._queue.shift());
246
+ entry.messageId = (connection.queueEntry.mail.message.getHeader('message-id') || '').replace(/[<>\s]/g, '');
247
+
248
+ connection.available = false;
249
+
250
+ this.logger.debug(
251
+ {
252
+ tnx: 'pool',
253
+ cid: connection.id,
254
+ messageId: entry.messageId,
255
+ action: 'assign'
256
+ },
257
+ 'Assigned message <%s> to #%s (%s)',
258
+ entry.messageId,
259
+ connection.id,
260
+ connection.messages + 1
261
+ );
262
+
263
+ if (this._rateLimit.limit) {
264
+ this._rateLimit.counter++;
265
+ if (!this._rateLimit.checkpoint) {
266
+ this._rateLimit.checkpoint = Date.now();
267
+ }
268
+ }
269
+
270
+ connection.send(entry.mail, (err, info) => {
271
+ // only process callback if current handler is not changed
272
+ if (entry === connection.queueEntry) {
273
+ try {
274
+ entry.callback(err, info);
275
+ } catch (E) {
276
+ this.logger.error(
277
+ {
278
+ err: E,
279
+ tnx: 'callback',
280
+ cid: connection.id
281
+ },
282
+ 'Callback error for #%s: %s',
283
+ connection.id,
284
+ E.message
285
+ );
286
+ }
287
+ connection.queueEntry = false;
288
+ }
289
+ });
290
+ }
291
+
292
+ /**
293
+ * Creates a new pool resource
294
+ */
295
+ _createConnection() {
296
+ let connection = new PoolResource(this);
297
+
298
+ connection.id = ++this._connectionCounter;
299
+
300
+ this.logger.info(
301
+ {
302
+ tnx: 'pool',
303
+ cid: connection.id,
304
+ action: 'conection'
305
+ },
306
+ 'Created new pool resource #%s',
307
+ connection.id
308
+ );
309
+
310
+ // resource comes available
311
+ connection.on('available', () => {
312
+ this.logger.debug(
313
+ {
314
+ tnx: 'connection',
315
+ cid: connection.id,
316
+ action: 'available'
317
+ },
318
+ 'Connection #%s became available',
319
+ connection.id
320
+ );
321
+
322
+ if (this._closed) {
323
+ // if already closed run close() that will remove this connections from connections list
324
+ this.close();
325
+ } else {
326
+ // check if there's anything else to send
327
+ this._processMessages();
328
+ }
329
+ });
330
+
331
+ // resource is terminated with an error
332
+ connection.once('error', err => {
333
+ if (err.code !== 'EMAXLIMIT') {
334
+ this.logger.error(
335
+ {
336
+ err,
337
+ tnx: 'pool',
338
+ cid: connection.id
339
+ },
340
+ 'Pool Error for #%s: %s',
341
+ connection.id,
342
+ err.message
343
+ );
344
+ } else {
345
+ this.logger.debug(
346
+ {
347
+ tnx: 'pool',
348
+ cid: connection.id,
349
+ action: 'maxlimit'
350
+ },
351
+ 'Max messages limit exchausted for #%s',
352
+ connection.id
353
+ );
354
+ }
355
+
356
+ if (connection.queueEntry) {
357
+ try {
358
+ connection.queueEntry.callback(err);
359
+ } catch (E) {
360
+ this.logger.error(
361
+ {
362
+ err: E,
363
+ tnx: 'callback',
364
+ cid: connection.id
365
+ },
366
+ 'Callback error for #%s: %s',
367
+ connection.id,
368
+ E.message
369
+ );
370
+ }
371
+ connection.queueEntry = false;
372
+ }
373
+
374
+ // remove the erroneus connection from connections list
375
+ this._removeConnection(connection);
376
+
377
+ this._continueProcessing();
378
+ });
379
+
380
+ connection.once('close', () => {
381
+ this.logger.info(
382
+ {
383
+ tnx: 'connection',
384
+ cid: connection.id,
385
+ action: 'closed'
386
+ },
387
+ 'Connection #%s was closed',
388
+ connection.id
389
+ );
390
+
391
+ this._removeConnection(connection);
392
+
393
+ if (connection.queueEntry) {
394
+ // If the connection closed when sending, add the message to the queue again
395
+ // if max number of requeues is not reached yet
396
+ // Note that we must wait a bit.. because the callback of the 'error' handler might be called
397
+ // in the next event loop
398
+ setTimeout(() => {
399
+ if (connection.queueEntry) {
400
+ if (this._shouldRequeuOnConnectionClose(connection.queueEntry)) {
401
+ this._requeueEntryOnConnectionClose(connection);
402
+ } else {
403
+ this._failDeliveryOnConnectionClose(connection);
404
+ }
405
+ }
406
+ this._continueProcessing();
407
+ }, 50);
408
+ } else {
409
+ this._continueProcessing();
410
+ }
411
+ });
412
+
413
+ this._connections.push(connection);
414
+
415
+ return connection;
416
+ }
417
+
418
+ _shouldRequeuOnConnectionClose(queueEntry) {
419
+ if (this.options.maxRequeues === undefined || this.options.maxRequeues < 0) {
420
+ return true;
421
+ }
422
+
423
+ return queueEntry.requeueAttempts < this.options.maxRequeues;
424
+ }
425
+
426
+ _failDeliveryOnConnectionClose(connection) {
427
+ if (connection.queueEntry && connection.queueEntry.callback) {
428
+ try {
429
+ connection.queueEntry.callback(new Error('Reached maximum number of retries after connection was closed'));
430
+ } catch (E) {
431
+ this.logger.error(
432
+ {
433
+ err: E,
434
+ tnx: 'callback',
435
+ messageId: connection.queueEntry.messageId,
436
+ cid: connection.id
437
+ },
438
+ 'Callback error for #%s: %s',
439
+ connection.id,
440
+ E.message
441
+ );
442
+ }
443
+ connection.queueEntry = false;
444
+ }
445
+ }
446
+
447
+ _requeueEntryOnConnectionClose(connection) {
448
+ connection.queueEntry.requeueAttempts = connection.queueEntry.requeueAttempts + 1;
449
+ this.logger.debug(
450
+ {
451
+ tnx: 'pool',
452
+ cid: connection.id,
453
+ messageId: connection.queueEntry.messageId,
454
+ action: 'requeue'
455
+ },
456
+ 'Re-queued message <%s> for #%s. Attempt: #%s',
457
+ connection.queueEntry.messageId,
458
+ connection.id,
459
+ connection.queueEntry.requeueAttempts
460
+ );
461
+ this._queue.unshift(connection.queueEntry);
462
+ connection.queueEntry = false;
463
+ }
464
+
465
+ /**
466
+ * Continue to process message if the pool hasn't closed
467
+ */
468
+ _continueProcessing() {
469
+ if (this._closed) {
470
+ this.close();
471
+ } else {
472
+ setTimeout(() => this._processMessages(), 100);
473
+ }
474
+ }
475
+
476
+ /**
477
+ * Remove resource from pool
478
+ *
479
+ * @param {Object} connection The PoolResource to remove
480
+ */
481
+ _removeConnection(connection) {
482
+ let index = this._connections.indexOf(connection);
483
+
484
+ if (index !== -1) {
485
+ this._connections.splice(index, 1);
486
+ }
487
+ }
488
+
489
+ /**
490
+ * Checks if connections have hit current rate limit and if so, queues the availability callback
491
+ *
492
+ * @param {Function} callback Callback function to run once rate limiter has been cleared
493
+ */
494
+ _checkRateLimit(callback) {
495
+ if (!this._rateLimit.limit) {
496
+ return callback();
497
+ }
498
+
499
+ let now = Date.now();
500
+
501
+ if (this._rateLimit.counter < this._rateLimit.limit) {
502
+ return callback();
503
+ }
504
+
505
+ this._rateLimit.waiting.push(callback);
506
+
507
+ if (this._rateLimit.checkpoint <= now - this._rateLimit.delta) {
508
+ return this._clearRateLimit();
509
+ } else if (!this._rateLimit.timeout) {
510
+ this._rateLimit.timeout = setTimeout(() => this._clearRateLimit(), this._rateLimit.delta - (now - this._rateLimit.checkpoint));
511
+ this._rateLimit.checkpoint = now;
512
+ }
513
+ }
514
+
515
+ /**
516
+ * Clears current rate limit limitation and runs paused callback
517
+ */
518
+ _clearRateLimit() {
519
+ clearTimeout(this._rateLimit.timeout);
520
+ this._rateLimit.timeout = null;
521
+ this._rateLimit.counter = 0;
522
+ this._rateLimit.checkpoint = false;
523
+
524
+ // resume all paused connections
525
+ while (this._rateLimit.waiting.length) {
526
+ let cb = this._rateLimit.waiting.shift();
527
+ setImmediate(cb);
528
+ }
529
+ }
530
+
531
+ /**
532
+ * Returns true if there are free slots in the queue
533
+ */
534
+ isIdle() {
535
+ return this.idling;
536
+ }
537
+
538
+ /**
539
+ * Verifies SMTP configuration
540
+ *
541
+ * @param {Function} callback Callback function
542
+ */
543
+ verify(callback) {
544
+ let promise;
545
+
546
+ if (!callback) {
547
+ promise = new Promise((resolve, reject) => {
548
+ callback = shared.callbackPromise(resolve, reject);
549
+ });
550
+ }
551
+
552
+ let auth = new PoolResource(this).auth;
553
+
554
+ this.getSocket(this.options, (err, socketOptions) => {
555
+ if (err) {
556
+ return callback(err);
557
+ }
558
+
559
+ let options = this.options;
560
+ if (socketOptions && socketOptions.connection) {
561
+ this.logger.info(
562
+ {
563
+ tnx: 'proxy',
564
+ remoteAddress: socketOptions.connection.remoteAddress,
565
+ remotePort: socketOptions.connection.remotePort,
566
+ destHost: options.host || '',
567
+ destPort: options.port || '',
568
+ action: 'connected'
569
+ },
570
+ 'Using proxied socket from %s:%s to %s:%s',
571
+ socketOptions.connection.remoteAddress,
572
+ socketOptions.connection.remotePort,
573
+ options.host || '',
574
+ options.port || ''
575
+ );
576
+ options = shared.assign(false, options);
577
+ Object.keys(socketOptions).forEach(key => {
578
+ options[key] = socketOptions[key];
579
+ });
580
+ }
581
+
582
+ let connection = new SMTPConnection(options);
583
+ let returned = false;
584
+
585
+ connection.once('error', err => {
586
+ if (returned) {
587
+ return;
588
+ }
589
+ returned = true;
590
+ connection.close();
591
+ return callback(err);
592
+ });
593
+
594
+ connection.once('end', () => {
595
+ if (returned) {
596
+ return;
597
+ }
598
+ returned = true;
599
+ return callback(new Error('Connection closed'));
600
+ });
601
+
602
+ let finalize = () => {
603
+ if (returned) {
604
+ return;
605
+ }
606
+ returned = true;
607
+ connection.quit();
608
+ return callback(null, true);
609
+ };
610
+
611
+ connection.connect(() => {
612
+ if (returned) {
613
+ return;
614
+ }
615
+
616
+ if (auth && (connection.allowsAuth || options.forceAuth)) {
617
+ connection.login(auth, err => {
618
+ if (returned) {
619
+ return;
620
+ }
621
+
622
+ if (err) {
623
+ returned = true;
624
+ connection.close();
625
+ return callback(err);
626
+ }
627
+
628
+ finalize();
629
+ });
630
+ } else if (!auth && connection.allowsAuth && options.forceAuth) {
631
+ let err = new Error('Authentication info was not provided');
632
+ err.code = 'NoAuth';
633
+
634
+ returned = true;
635
+ connection.close();
636
+ return callback(err);
637
+ } else {
638
+ finalize();
639
+ }
640
+ });
641
+ });
642
+
643
+ return promise;
644
+ }
645
+ }
646
+
647
+ // expose to the world
648
+ module.exports = SMTPPool;