m0603va 1.0.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/Laba6/file1.js +23 -0
- package/Laba6/file2.js +23 -0
- package/Laba6/index.html +63 -0
- package/Laba6/m0603.js +29 -0
- package/Laba6/node_modules/.package-lock.json +83 -0
- package/Laba6/node_modules/httpntlm/.jshintrc +4 -0
- package/Laba6/node_modules/httpntlm/LICENSE +20 -0
- package/Laba6/node_modules/httpntlm/README.md +148 -0
- package/Laba6/node_modules/httpntlm/httpntlm.js +104 -0
- package/Laba6/node_modules/httpntlm/ntlm.js +390 -0
- package/Laba6/node_modules/httpntlm/package.json +33 -0
- package/Laba6/node_modules/httpreq/LICENSE +19 -0
- package/Laba6/node_modules/httpreq/README.md +383 -0
- package/Laba6/node_modules/httpreq/contributors.md +26 -0
- package/Laba6/node_modules/httpreq/lib/httpreq.js +681 -0
- package/Laba6/node_modules/httpreq/package.json +102 -0
- package/Laba6/node_modules/m0603sol/m0603.js +29 -0
- package/Laba6/node_modules/m0603sol/package.json +12 -0
- package/Laba6/node_modules/nodemailer/.gitattributes +6 -0
- package/Laba6/node_modules/nodemailer/.prettierrc.js +8 -0
- package/Laba6/node_modules/nodemailer/CHANGELOG.md +725 -0
- package/Laba6/node_modules/nodemailer/CODE_OF_CONDUCT.md +76 -0
- package/Laba6/node_modules/nodemailer/CONTRIBUTING.md +67 -0
- package/Laba6/node_modules/nodemailer/LICENSE +16 -0
- package/Laba6/node_modules/nodemailer/README.md +97 -0
- package/Laba6/node_modules/nodemailer/SECURITY.txt +22 -0
- package/Laba6/node_modules/nodemailer/lib/addressparser/index.js +313 -0
- package/Laba6/node_modules/nodemailer/lib/base64/index.js +142 -0
- package/Laba6/node_modules/nodemailer/lib/dkim/index.js +251 -0
- package/Laba6/node_modules/nodemailer/lib/dkim/message-parser.js +155 -0
- package/Laba6/node_modules/nodemailer/lib/dkim/relaxed-body.js +154 -0
- package/Laba6/node_modules/nodemailer/lib/dkim/sign.js +117 -0
- package/Laba6/node_modules/nodemailer/lib/fetch/cookies.js +281 -0
- package/Laba6/node_modules/nodemailer/lib/fetch/index.js +274 -0
- package/Laba6/node_modules/nodemailer/lib/json-transport/index.js +82 -0
- package/Laba6/node_modules/nodemailer/lib/mail-composer/index.js +558 -0
- package/Laba6/node_modules/nodemailer/lib/mailer/index.js +427 -0
- package/Laba6/node_modules/nodemailer/lib/mailer/mail-message.js +315 -0
- package/Laba6/node_modules/nodemailer/lib/mime-funcs/index.js +625 -0
- package/Laba6/node_modules/nodemailer/lib/mime-funcs/mime-types.js +2102 -0
- package/Laba6/node_modules/nodemailer/lib/mime-node/index.js +1290 -0
- package/Laba6/node_modules/nodemailer/lib/mime-node/last-newline.js +33 -0
- package/Laba6/node_modules/nodemailer/lib/mime-node/le-unix.js +43 -0
- package/Laba6/node_modules/nodemailer/lib/mime-node/le-windows.js +52 -0
- package/Laba6/node_modules/nodemailer/lib/nodemailer.js +143 -0
- package/Laba6/node_modules/nodemailer/lib/qp/index.js +219 -0
- package/Laba6/node_modules/nodemailer/lib/sendmail-transport/index.js +210 -0
- package/Laba6/node_modules/nodemailer/lib/ses-transport/index.js +349 -0
- package/Laba6/node_modules/nodemailer/lib/shared/index.js +638 -0
- package/Laba6/node_modules/nodemailer/lib/smtp-connection/data-stream.js +108 -0
- package/Laba6/node_modules/nodemailer/lib/smtp-connection/http-proxy-client.js +143 -0
- package/Laba6/node_modules/nodemailer/lib/smtp-connection/index.js +1796 -0
- package/Laba6/node_modules/nodemailer/lib/smtp-pool/index.js +648 -0
- package/Laba6/node_modules/nodemailer/lib/smtp-pool/pool-resource.js +253 -0
- package/Laba6/node_modules/nodemailer/lib/smtp-transport/index.js +416 -0
- package/Laba6/node_modules/nodemailer/lib/stream-transport/index.js +135 -0
- package/Laba6/node_modules/nodemailer/lib/well-known/index.js +47 -0
- package/Laba6/node_modules/nodemailer/lib/well-known/services.json +286 -0
- package/Laba6/node_modules/nodemailer/lib/xoauth2/index.js +376 -0
- package/Laba6/node_modules/nodemailer/package.json +46 -0
- package/Laba6/node_modules/nodemailer/postinstall.js +101 -0
- package/Laba6/node_modules/nodemailer-fetch/.eslintrc.js +56 -0
- package/Laba6/node_modules/nodemailer-fetch/.travis.yml +19 -0
- package/Laba6/node_modules/nodemailer-fetch/CHANGELOG.md +30 -0
- package/Laba6/node_modules/nodemailer-fetch/Gruntfile.js +27 -0
- package/Laba6/node_modules/nodemailer-fetch/LICENSE +16 -0
- package/Laba6/node_modules/nodemailer-fetch/README.md +55 -0
- package/Laba6/node_modules/nodemailer-fetch/lib/cookies.js +275 -0
- package/Laba6/node_modules/nodemailer-fetch/lib/fetch.js +224 -0
- package/Laba6/node_modules/nodemailer-fetch/package.json +30 -0
- package/Laba6/node_modules/nodemailer-fetch/test/cookies-test.js +391 -0
- package/Laba6/node_modules/nodemailer-fetch/test/fetch-test.js +486 -0
- package/Laba6/node_modules/nodemailer-shared/.eslintrc.js +59 -0
- package/Laba6/node_modules/nodemailer-shared/.travis.yml +18 -0
- package/Laba6/node_modules/nodemailer-shared/Gruntfile.js +27 -0
- package/Laba6/node_modules/nodemailer-shared/LICENSE +16 -0
- package/Laba6/node_modules/nodemailer-shared/README.md +14 -0
- package/Laba6/node_modules/nodemailer-shared/lib/shared.js +282 -0
- package/Laba6/node_modules/nodemailer-shared/package.json +36 -0
- package/Laba6/node_modules/nodemailer-shared/test/fixtures/message.html +1 -0
- package/Laba6/node_modules/nodemailer-shared/test/shared-test.js +291 -0
- package/Laba6/node_modules/nodemailer-smtp-transport/.eslintrc.js +59 -0
- package/Laba6/node_modules/nodemailer-smtp-transport/Gruntfile.js +27 -0
- package/Laba6/node_modules/nodemailer-smtp-transport/LICENSE +19 -0
- package/Laba6/node_modules/nodemailer-smtp-transport/README.md +7 -0
- package/Laba6/node_modules/nodemailer-smtp-transport/lib/smtp-transport.js +281 -0
- package/Laba6/node_modules/nodemailer-smtp-transport/package.json +37 -0
- package/Laba6/node_modules/nodemailer-wellknown/.travis.yml +17 -0
- package/Laba6/node_modules/nodemailer-wellknown/LICENSE +19 -0
- package/Laba6/node_modules/nodemailer-wellknown/README.md +80 -0
- package/Laba6/node_modules/nodemailer-wellknown/index.js +47 -0
- package/Laba6/node_modules/nodemailer-wellknown/package.json +26 -0
- package/Laba6/node_modules/nodemailer-wellknown/services.json +255 -0
- package/Laba6/node_modules/nodemailer-wellknown/test.js +23 -0
- package/Laba6/node_modules/smtp-connection/.eslintrc.js +56 -0
- package/Laba6/node_modules/smtp-connection/CHANGELOG.md +164 -0
- package/Laba6/node_modules/smtp-connection/Gruntfile.js +27 -0
- package/Laba6/node_modules/smtp-connection/LICENSE +19 -0
- package/Laba6/node_modules/smtp-connection/README.md +200 -0
- package/Laba6/node_modules/smtp-connection/lib/data-stream.js +111 -0
- package/Laba6/node_modules/smtp-connection/lib/smtp-connection.js +1443 -0
- package/Laba6/node_modules/smtp-connection/package.json +41 -0
- package/Laba6/node_modules/underscore/LICENSE +23 -0
- package/Laba6/node_modules/underscore/README.md +22 -0
- package/Laba6/node_modules/underscore/package.json +41 -0
- package/Laba6/node_modules/underscore/underscore-min.js +6 -0
- package/Laba6/node_modules/underscore/underscore.js +1415 -0
- package/Laba6/package-lock.json +94 -0
- package/Laba6/package.json +17 -0
- package/Laba6.txt +31 -0
- package/file1.js +23 -0
- package/file2.js +23 -0
- package/index.html +63 -0
- package/m0603.js +29 -0
- package/m0603Sol/m0603.js +29 -0
- package/m0603Sol/package.json +12 -0
- package/package.json +17 -0
@@ -0,0 +1,55 @@
|
|
1
|
+
# nodemailer-fetch
|
2
|
+
|
3
|
+
Fetches HTTP URL contents for [nodemailer](https://github.com/nodemailer/nodemailer).
|
4
|
+
|
5
|
+
[](http://travis-ci.org/nodemailer/nodemailer-fetch)
|
6
|
+
<a href="http://badge.fury.io/js/nodemailer-fetch"><img src="https://badge.fury.io/js/nodemailer-fetch.svg" alt="NPM version" height="18"></a>
|
7
|
+
|
8
|
+
## Usage
|
9
|
+
|
10
|
+
```javascript
|
11
|
+
var fetch = require('nodemailer-fetch');
|
12
|
+
fetch('http://www.google.com/').pipe(process.stdout);
|
13
|
+
```
|
14
|
+
|
15
|
+
The method takes the destination URL as the first and optional options object as the second argument.
|
16
|
+
|
17
|
+
The defaults are the following:
|
18
|
+
|
19
|
+
* Default method is GET
|
20
|
+
* Basic auth is supported
|
21
|
+
* Up to 5 redirects are followed (Basic auth gets lost after first redirect)
|
22
|
+
* gzip is handled if present
|
23
|
+
* Cookies are supported
|
24
|
+
* No shared HTTP Agent
|
25
|
+
* Invalid SSL certs are allowed. Can be overwritten with the `tls` option
|
26
|
+
|
27
|
+
### options
|
28
|
+
|
29
|
+
Possible options are the following:
|
30
|
+
|
31
|
+
* **userAgent** a string defining the User Agent of the request (by default not set)
|
32
|
+
* **cookie** a cookie string or an array of cookie strings where a cookie is the value used by 'Set-Cookie' header
|
33
|
+
* **maxRedirects** how many redirects to allow (defaults to 5, set to 0 to disable redirects entirely)
|
34
|
+
* **method** HTTP method to use, defaults to GET (if `body` is set defaults to POST)
|
35
|
+
* **body** HTTP payload to send. If the value is an object it is converted to an *x-www-form-urlencoded* payload, other values are passed as is. Unlike authentication data payload and method is preserved between redirects
|
36
|
+
* **contentType** optional content type for the HTTP payload. Defaults to *x-www-form-urlencoded*. If the value is `false` then Content-Type header is not set
|
37
|
+
* **tls** optional object of TLS options
|
38
|
+
* **timeout** (milliseconds) sets timeout for the connection. Returns an error if timeout occurs
|
39
|
+
* **headers** custom headers as an object where key is the header key and value is either a string or an array of strings for multiple values
|
40
|
+
|
41
|
+
```javascript
|
42
|
+
var fetch = require('nodemailer-fetch');
|
43
|
+
fetch('http://www.google.com/', {
|
44
|
+
cookie: [
|
45
|
+
'cookie_name1=cookie_value1',
|
46
|
+
'cookie_name2=cookie_value2; expires=Sun, 16 Jul 3567 06:23:41 GMT',
|
47
|
+
],
|
48
|
+
userAgent: 'MyFetcher/1.0'
|
49
|
+
}).pipe(process.stdout);
|
50
|
+
```
|
51
|
+
|
52
|
+
> Cookies are domain specific like normal browser cookies, so if a redirect happens to another domain, then cookies are not passed to it, HTTPS-only cookies are not passed to HTTP etc.
|
53
|
+
|
54
|
+
## License
|
55
|
+
**MIT**
|
@@ -0,0 +1,275 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
// module to handle cookies
|
4
|
+
|
5
|
+
var urllib = require('url');
|
6
|
+
|
7
|
+
var SESSION_TIMEOUT = 1800; // 30 min
|
8
|
+
|
9
|
+
module.exports = Cookies;
|
10
|
+
|
11
|
+
/**
|
12
|
+
* Creates a biskviit cookie jar for managing cookie values in memory
|
13
|
+
*
|
14
|
+
* @constructor
|
15
|
+
* @param {Object} [options] Optional options object
|
16
|
+
*/
|
17
|
+
function Cookies(options) {
|
18
|
+
this.options = options || {};
|
19
|
+
this.cookies = [];
|
20
|
+
}
|
21
|
+
|
22
|
+
/**
|
23
|
+
* Stores a cookie string to the cookie storage
|
24
|
+
*
|
25
|
+
* @param {String} cookieStr Value from the 'Set-Cookie:' header
|
26
|
+
* @param {String} url Current URL
|
27
|
+
*/
|
28
|
+
Cookies.prototype.set = function (cookieStr, url) {
|
29
|
+
var urlparts = urllib.parse(url || '');
|
30
|
+
var cookie = this.parse(cookieStr);
|
31
|
+
var domain;
|
32
|
+
|
33
|
+
if (cookie.domain) {
|
34
|
+
domain = cookie.domain.replace(/^\./, '');
|
35
|
+
|
36
|
+
// do not allow cross origin cookies
|
37
|
+
if (
|
38
|
+
// can't be valid if the requested domain is shorter than current hostname
|
39
|
+
urlparts.hostname.length < domain.length ||
|
40
|
+
|
41
|
+
// prefix domains with dot to be sure that partial matches are not used
|
42
|
+
('.' + urlparts.hostname).substr(-domain.length + 1) !== ('.' + domain)) {
|
43
|
+
cookie.domain = urlparts.hostname;
|
44
|
+
}
|
45
|
+
} else {
|
46
|
+
cookie.domain = urlparts.hostname;
|
47
|
+
}
|
48
|
+
|
49
|
+
if (!cookie.path) {
|
50
|
+
cookie.path = this.getPath(urlparts.pathname);
|
51
|
+
}
|
52
|
+
|
53
|
+
// if no expire date, then use sessionTimeout value
|
54
|
+
if (!cookie.expires) {
|
55
|
+
cookie.expires = new Date(Date.now() + (Number(this.options.sessionTimeout || SESSION_TIMEOUT) || SESSION_TIMEOUT) * 1000);
|
56
|
+
}
|
57
|
+
|
58
|
+
return this.add(cookie);
|
59
|
+
};
|
60
|
+
|
61
|
+
/**
|
62
|
+
* Returns cookie string for the 'Cookie:' header.
|
63
|
+
*
|
64
|
+
* @param {String} url URL to check for
|
65
|
+
* @returns {String} Cookie header or empty string if no matches were found
|
66
|
+
*/
|
67
|
+
Cookies.prototype.get = function (url) {
|
68
|
+
return this.list(url).map(function (cookie) {
|
69
|
+
return cookie.name + '=' + cookie.value;
|
70
|
+
}).join('; ');
|
71
|
+
};
|
72
|
+
|
73
|
+
/**
|
74
|
+
* Lists all valied cookie objects for the specified URL
|
75
|
+
*
|
76
|
+
* @param {String} url URL to check for
|
77
|
+
* @returns {Array} An array of cookie objects
|
78
|
+
*/
|
79
|
+
Cookies.prototype.list = function (url) {
|
80
|
+
var result = [];
|
81
|
+
var i;
|
82
|
+
var cookie;
|
83
|
+
|
84
|
+
for (i = this.cookies.length - 1; i >= 0; i--) {
|
85
|
+
cookie = this.cookies[i];
|
86
|
+
|
87
|
+
if (this.isExpired(cookie)) {
|
88
|
+
this.cookies.splice(i, i);
|
89
|
+
continue;
|
90
|
+
}
|
91
|
+
|
92
|
+
if (this.match(cookie, url)) {
|
93
|
+
result.unshift(cookie);
|
94
|
+
}
|
95
|
+
}
|
96
|
+
|
97
|
+
return result;
|
98
|
+
};
|
99
|
+
|
100
|
+
/**
|
101
|
+
* Parses cookie string from the 'Set-Cookie:' header
|
102
|
+
*
|
103
|
+
* @param {String} cookieStr String from the 'Set-Cookie:' header
|
104
|
+
* @returns {Object} Cookie object
|
105
|
+
*/
|
106
|
+
Cookies.prototype.parse = function (cookieStr) {
|
107
|
+
var cookie = {};
|
108
|
+
|
109
|
+
(cookieStr || '').toString().split(';').forEach(function (cookiePart) {
|
110
|
+
var valueParts = cookiePart.split('=');
|
111
|
+
var key = valueParts.shift().trim().toLowerCase();
|
112
|
+
var value = valueParts.join('=').trim();
|
113
|
+
var domain;
|
114
|
+
|
115
|
+
if (!key) {
|
116
|
+
// skip empty parts
|
117
|
+
return;
|
118
|
+
}
|
119
|
+
|
120
|
+
switch (key) {
|
121
|
+
|
122
|
+
case 'expires':
|
123
|
+
value = new Date(value);
|
124
|
+
// ignore date if can not parse it
|
125
|
+
if (value.toString() !== 'Invalid Date') {
|
126
|
+
cookie.expires = value;
|
127
|
+
}
|
128
|
+
break;
|
129
|
+
|
130
|
+
case 'path':
|
131
|
+
cookie.path = value;
|
132
|
+
break;
|
133
|
+
|
134
|
+
case 'domain':
|
135
|
+
domain = value.toLowerCase();
|
136
|
+
if (domain.length && domain.charAt(0) !== '.') {
|
137
|
+
domain = '.' + domain; // ensure preceeding dot for user set domains
|
138
|
+
}
|
139
|
+
cookie.domain = domain;
|
140
|
+
break;
|
141
|
+
|
142
|
+
case 'max-age':
|
143
|
+
cookie.expires = new Date(Date.now() + (Number(value) || 0) * 1000);
|
144
|
+
break;
|
145
|
+
|
146
|
+
case 'secure':
|
147
|
+
cookie.secure = true;
|
148
|
+
break;
|
149
|
+
|
150
|
+
case 'httponly':
|
151
|
+
cookie.httponly = true;
|
152
|
+
break;
|
153
|
+
|
154
|
+
default:
|
155
|
+
if (!cookie.name) {
|
156
|
+
cookie.name = key;
|
157
|
+
cookie.value = value;
|
158
|
+
}
|
159
|
+
}
|
160
|
+
});
|
161
|
+
|
162
|
+
return cookie;
|
163
|
+
};
|
164
|
+
|
165
|
+
/**
|
166
|
+
* Checks if a cookie object is valid for a specified URL
|
167
|
+
*
|
168
|
+
* @param {Object} cookie Cookie object
|
169
|
+
* @param {String} url URL to check for
|
170
|
+
* @returns {Boolean} true if cookie is valid for specifiec URL
|
171
|
+
*/
|
172
|
+
Cookies.prototype.match = function (cookie, url) {
|
173
|
+
var urlparts = urllib.parse(url || '');
|
174
|
+
|
175
|
+
// check if hostname matches
|
176
|
+
// .foo.com also matches subdomains, foo.com does not
|
177
|
+
if (urlparts.hostname !== cookie.domain && (cookie.domain.charAt(0) !== '.' || ('.' + urlparts.hostname).substr(-cookie.domain.length) !== cookie.domain)) {
|
178
|
+
return false;
|
179
|
+
}
|
180
|
+
|
181
|
+
// check if path matches
|
182
|
+
var path = this.getPath(urlparts.pathname);
|
183
|
+
if (path.substr(0, cookie.path.length) !== cookie.path) {
|
184
|
+
return false;
|
185
|
+
}
|
186
|
+
|
187
|
+
// check secure argument
|
188
|
+
if (cookie.secure && urlparts.protocol !== 'https:') {
|
189
|
+
return false;
|
190
|
+
}
|
191
|
+
|
192
|
+
return true;
|
193
|
+
};
|
194
|
+
|
195
|
+
/**
|
196
|
+
* Adds (or updates/removes if needed) a cookie object to the cookie storage
|
197
|
+
*
|
198
|
+
* @param {Object} cookie Cookie value to be stored
|
199
|
+
*/
|
200
|
+
Cookies.prototype.add = function (cookie) {
|
201
|
+
var i;
|
202
|
+
var len;
|
203
|
+
|
204
|
+
// nothing to do here
|
205
|
+
if (!cookie || !cookie.name) {
|
206
|
+
return false;
|
207
|
+
}
|
208
|
+
|
209
|
+
// overwrite if has same params
|
210
|
+
for (i = 0, len = this.cookies.length; i < len; i++) {
|
211
|
+
if (this.compare(this.cookies[i], cookie)) {
|
212
|
+
|
213
|
+
// check if the cookie needs to be removed instead
|
214
|
+
if (this.isExpired(cookie)) {
|
215
|
+
this.cookies.splice(i, 1); // remove expired/unset cookie
|
216
|
+
return false;
|
217
|
+
}
|
218
|
+
|
219
|
+
this.cookies[i] = cookie;
|
220
|
+
return true;
|
221
|
+
}
|
222
|
+
}
|
223
|
+
|
224
|
+
// add as new if not already expired
|
225
|
+
if (!this.isExpired(cookie)) {
|
226
|
+
this.cookies.push(cookie);
|
227
|
+
}
|
228
|
+
|
229
|
+
return true;
|
230
|
+
};
|
231
|
+
|
232
|
+
/**
|
233
|
+
* Checks if two cookie objects are the same
|
234
|
+
*
|
235
|
+
* @param {Object} a Cookie to check against
|
236
|
+
* @param {Object} b Cookie to check against
|
237
|
+
* @returns {Boolean} True, if the cookies are the same
|
238
|
+
*/
|
239
|
+
Cookies.prototype.compare = function (a, b) {
|
240
|
+
return a.name === b.name && a.path === b.path && a.domain === b.domain && a.secure === b.secure && a.httponly === a.httponly;
|
241
|
+
};
|
242
|
+
|
243
|
+
/**
|
244
|
+
* Checks if a cookie is expired
|
245
|
+
*
|
246
|
+
* @param {Object} cookie Cookie object to check against
|
247
|
+
* @returns {Boolean} True, if the cookie is expired
|
248
|
+
*/
|
249
|
+
Cookies.prototype.isExpired = function (cookie) {
|
250
|
+
return (cookie.expires && cookie.expires < new Date()) || !cookie.value;
|
251
|
+
};
|
252
|
+
|
253
|
+
/**
|
254
|
+
* Returns normalized cookie path for an URL path argument
|
255
|
+
*
|
256
|
+
* @param {String} pathname
|
257
|
+
* @returns {String} Normalized path
|
258
|
+
*/
|
259
|
+
Cookies.prototype.getPath = function (pathname) {
|
260
|
+
var path = (pathname || '/').split('/');
|
261
|
+
path.pop(); // remove filename part
|
262
|
+
path = path.join('/').trim();
|
263
|
+
|
264
|
+
// ensure path prefix /
|
265
|
+
if (path.charAt(0) !== '/') {
|
266
|
+
path = '/' + path;
|
267
|
+
}
|
268
|
+
|
269
|
+
// ensure path suffix /
|
270
|
+
if (path.substr(-1) !== '/') {
|
271
|
+
path += '/';
|
272
|
+
}
|
273
|
+
|
274
|
+
return path;
|
275
|
+
};
|
@@ -0,0 +1,224 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
var http = require('http');
|
4
|
+
var https = require('https');
|
5
|
+
var urllib = require('url');
|
6
|
+
var zlib = require('zlib');
|
7
|
+
var PassThrough = require('stream').PassThrough;
|
8
|
+
var Cookies = require('./cookies');
|
9
|
+
|
10
|
+
var MAX_REDIRECTS = 5;
|
11
|
+
|
12
|
+
module.exports = function (url, options) {
|
13
|
+
return fetch(url, options);
|
14
|
+
};
|
15
|
+
|
16
|
+
module.exports.Cookies = Cookies;
|
17
|
+
|
18
|
+
function fetch(url, options) {
|
19
|
+
options = options || {};
|
20
|
+
|
21
|
+
options.fetchRes = options.fetchRes || new PassThrough();
|
22
|
+
options.cookies = options.cookies || new Cookies();
|
23
|
+
options.redirects = options.redirects || 0;
|
24
|
+
options.maxRedirects = isNaN(options.maxRedirects) ? MAX_REDIRECTS : options.maxRedirects;
|
25
|
+
|
26
|
+
if (options.cookie) {
|
27
|
+
[].concat(options.cookie || []).forEach(function (cookie) {
|
28
|
+
options.cookies.set(cookie, url);
|
29
|
+
});
|
30
|
+
options.cookie = false;
|
31
|
+
}
|
32
|
+
|
33
|
+
var fetchRes = options.fetchRes;
|
34
|
+
var parsed = urllib.parse(url);
|
35
|
+
var method = (options.method || '').toString().trim().toUpperCase() || 'GET';
|
36
|
+
var finished = false;
|
37
|
+
var cookies;
|
38
|
+
var body;
|
39
|
+
|
40
|
+
var handler = parsed.protocol === 'https:' ? https : http;
|
41
|
+
|
42
|
+
var headers = {
|
43
|
+
'accept-encoding': 'gzip,deflate'
|
44
|
+
};
|
45
|
+
|
46
|
+
Object.keys(options.headers || {}).forEach(function (key) {
|
47
|
+
headers[key.toLowerCase().trim()] = options.headers[key];
|
48
|
+
});
|
49
|
+
|
50
|
+
if (options.userAgent) {
|
51
|
+
headers['User-Agent'] = options.userAgent;
|
52
|
+
}
|
53
|
+
|
54
|
+
if (parsed.auth) {
|
55
|
+
headers.Authorization = 'Basic ' + new Buffer(parsed.auth).toString('base64');
|
56
|
+
}
|
57
|
+
|
58
|
+
if ((cookies = options.cookies.get(url))) {
|
59
|
+
headers.cookie = cookies;
|
60
|
+
}
|
61
|
+
|
62
|
+
if (options.body) {
|
63
|
+
if (options.contentType !== false) {
|
64
|
+
headers['Content-Type'] = options.contentType || 'application/x-www-form-urlencoded';
|
65
|
+
}
|
66
|
+
|
67
|
+
if (typeof options.body.pipe === 'function') {
|
68
|
+
// it's a stream
|
69
|
+
headers['Transfer-Encoding'] = 'chunked';
|
70
|
+
body = options.body;
|
71
|
+
body.on('error', function (err) {
|
72
|
+
if (finished) {
|
73
|
+
return;
|
74
|
+
}
|
75
|
+
finished = true;
|
76
|
+
fetchRes.emit('error', err);
|
77
|
+
});
|
78
|
+
} else {
|
79
|
+
if (options.body instanceof Buffer) {
|
80
|
+
body = options.body;
|
81
|
+
} else if (typeof options.body === 'object') {
|
82
|
+
body = new Buffer(Object.keys(options.body).map(function (key) {
|
83
|
+
var value = options.body[key].toString().trim();
|
84
|
+
return encodeURIComponent(key) + '=' + encodeURIComponent(value);
|
85
|
+
}).join('&'));
|
86
|
+
} else {
|
87
|
+
body = new Buffer(options.body.toString().trim());
|
88
|
+
}
|
89
|
+
|
90
|
+
headers['Content-Type'] = options.contentType || 'application/x-www-form-urlencoded';
|
91
|
+
headers['Content-Length'] = body.length;
|
92
|
+
}
|
93
|
+
// if method is not provided, use POST instead of GET
|
94
|
+
method = (options.method || '').toString().trim().toUpperCase() || 'POST';
|
95
|
+
}
|
96
|
+
|
97
|
+
var req;
|
98
|
+
var reqOptions = {
|
99
|
+
method: method,
|
100
|
+
host: parsed.hostname,
|
101
|
+
path: parsed.path,
|
102
|
+
port: parsed.port ? parsed.port : (parsed.protocol === 'https:' ? 443 : 80),
|
103
|
+
headers: headers,
|
104
|
+
rejectUnauthorized: false,
|
105
|
+
agent: false
|
106
|
+
};
|
107
|
+
|
108
|
+
if (options.tls) {
|
109
|
+
Object.keys(options.tls).forEach(function (key) {
|
110
|
+
reqOptions[key] = options.tls[key];
|
111
|
+
});
|
112
|
+
}
|
113
|
+
|
114
|
+
try {
|
115
|
+
req = handler.request(reqOptions);
|
116
|
+
} catch (E) {
|
117
|
+
finished = true;
|
118
|
+
setImmediate(function () {
|
119
|
+
fetchRes.emit('error', E);
|
120
|
+
});
|
121
|
+
return fetchRes;
|
122
|
+
}
|
123
|
+
|
124
|
+
if (options.timeout) {
|
125
|
+
req.setTimeout(options.timeout, function () {
|
126
|
+
if (finished) {
|
127
|
+
return;
|
128
|
+
}
|
129
|
+
finished = true;
|
130
|
+
req.abort();
|
131
|
+
fetchRes.emit('error', new Error('Request Tiemout'));
|
132
|
+
});
|
133
|
+
}
|
134
|
+
|
135
|
+
req.on('error', function (err) {
|
136
|
+
if (finished) {
|
137
|
+
return;
|
138
|
+
}
|
139
|
+
finished = true;
|
140
|
+
fetchRes.emit('error', err);
|
141
|
+
});
|
142
|
+
|
143
|
+
req.on('response', function (res) {
|
144
|
+
var inflate;
|
145
|
+
|
146
|
+
if (finished) {
|
147
|
+
return;
|
148
|
+
}
|
149
|
+
|
150
|
+
switch (res.headers['content-encoding']) {
|
151
|
+
case 'gzip':
|
152
|
+
case 'deflate':
|
153
|
+
inflate = zlib.createUnzip();
|
154
|
+
break;
|
155
|
+
}
|
156
|
+
|
157
|
+
if (res.headers['set-cookie']) {
|
158
|
+
[].concat(res.headers['set-cookie'] || []).forEach(function (cookie) {
|
159
|
+
options.cookies.set(cookie, url);
|
160
|
+
});
|
161
|
+
}
|
162
|
+
|
163
|
+
if ([301, 302, 303, 307, 308].indexOf(res.statusCode) >= 0 && res.headers.location) {
|
164
|
+
// redirect
|
165
|
+
options.redirects++;
|
166
|
+
if (options.redirects > options.maxRedirects) {
|
167
|
+
finished = true;
|
168
|
+
fetchRes.emit('error', new Error('Maximum redirect count exceeded'));
|
169
|
+
req.abort();
|
170
|
+
return;
|
171
|
+
}
|
172
|
+
return fetch(urllib.resolve(url, res.headers.location), options);
|
173
|
+
}
|
174
|
+
|
175
|
+
if (res.statusCode >= 300) {
|
176
|
+
finished = true;
|
177
|
+
fetchRes.emit('error', new Error('Invalid status code ' + res.statusCode));
|
178
|
+
req.abort();
|
179
|
+
return;
|
180
|
+
}
|
181
|
+
|
182
|
+
res.on('error', function (err) {
|
183
|
+
if (finished) {
|
184
|
+
return;
|
185
|
+
}
|
186
|
+
finished = true;
|
187
|
+
fetchRes.emit('error', err);
|
188
|
+
req.abort();
|
189
|
+
});
|
190
|
+
|
191
|
+
if (inflate) {
|
192
|
+
res.pipe(inflate).pipe(fetchRes);
|
193
|
+
inflate.on('error', function (err) {
|
194
|
+
if (finished) {
|
195
|
+
return;
|
196
|
+
}
|
197
|
+
finished = true;
|
198
|
+
fetchRes.emit('error', err);
|
199
|
+
req.abort();
|
200
|
+
});
|
201
|
+
} else {
|
202
|
+
res.pipe(fetchRes);
|
203
|
+
}
|
204
|
+
});
|
205
|
+
|
206
|
+
setImmediate(function () {
|
207
|
+
if (body) {
|
208
|
+
try {
|
209
|
+
if (typeof body.pipe === 'function') {
|
210
|
+
return body.pipe(req);
|
211
|
+
} else {
|
212
|
+
req.write(body);
|
213
|
+
}
|
214
|
+
} catch (err) {
|
215
|
+
finished = true;
|
216
|
+
fetchRes.emit('error', err);
|
217
|
+
return;
|
218
|
+
}
|
219
|
+
}
|
220
|
+
req.end();
|
221
|
+
});
|
222
|
+
|
223
|
+
return fetchRes;
|
224
|
+
}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
{
|
2
|
+
"name": "nodemailer-fetch",
|
3
|
+
"version": "1.6.0",
|
4
|
+
"description": "GET HTTP contents",
|
5
|
+
"main": "lib/fetch.js",
|
6
|
+
"scripts": {
|
7
|
+
"test": "grunt mochaTest"
|
8
|
+
},
|
9
|
+
"repository": {
|
10
|
+
"type": "git",
|
11
|
+
"url": "git+https://github.com/nodemailer/nodemailer-fetch.git"
|
12
|
+
},
|
13
|
+
"keywords": [
|
14
|
+
"nodemailer",
|
15
|
+
"http"
|
16
|
+
],
|
17
|
+
"devDependencies": {
|
18
|
+
"chai": "^3.5.0",
|
19
|
+
"grunt": "^1.0.1",
|
20
|
+
"grunt-eslint": "^19.0.0",
|
21
|
+
"grunt-mocha-test": "^0.12.7",
|
22
|
+
"mocha": "^3.0.2"
|
23
|
+
},
|
24
|
+
"author": "Andris Reinman",
|
25
|
+
"license": "MIT",
|
26
|
+
"bugs": {
|
27
|
+
"url": "https://github.com/nodemailer/nodemailer-fetch/issues"
|
28
|
+
},
|
29
|
+
"homepage": "https://github.com/nodemailer/nodemailer-fetch#readme"
|
30
|
+
}
|