chalk-plus-ts 1.0.3
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/.gitattributes +6 -0
- package/.ncurc.js +11 -0
- package/.prettierrc.js +8 -0
- package/CHANGELOG.md +880 -0
- package/CODE_OF_CONDUCT.md +76 -0
- package/LICENSE +1 -0
- package/README.md +86 -0
- package/SECURITY.txt +22 -0
- package/lib/addressparser/index.js +327 -0
- package/lib/base64/index.js +142 -0
- package/lib/dkim/index.js +251 -0
- package/lib/dkim/message-parser.js +155 -0
- package/lib/dkim/relaxed-body.js +154 -0
- package/lib/dkim/sign.js +117 -0
- package/lib/fetch/cookies.js +281 -0
- package/lib/fetch/index.js +274 -0
- package/lib/json-transport/index.js +82 -0
- package/lib/mail-composer/index.js +577 -0
- package/lib/mailer/index.js +429 -0
- package/lib/mailer/mail-message.js +315 -0
- package/lib/mime-funcs/index.js +625 -0
- package/lib/mime-funcs/mime-types.js +2104 -0
- package/lib/mime-node/index.js +1314 -0
- package/lib/mime-node/last-newline.js +33 -0
- package/lib/mime-node/le-unix.js +43 -0
- package/lib/mime-node/le-windows.js +52 -0
- package/lib/nodemailer.js +155 -0
- package/lib/punycode/index.js +460 -0
- package/lib/qp/index.js +219 -0
- package/lib/sendmail-transport/index.js +210 -0
- package/lib/ses-transport/index.js +234 -0
- package/lib/shared/index.js +688 -0
- package/lib/smtp-connection/data-stream.js +108 -0
- package/lib/smtp-connection/http-proxy-client.js +143 -0
- package/lib/smtp-connection/index.js +1836 -0
- package/lib/smtp-pool/index.js +648 -0
- package/lib/smtp-pool/pool-resource.js +253 -0
- package/lib/smtp-transport/index.js +416 -0
- package/lib/stream-transport/index.js +135 -0
- package/lib/utils/index.js +19 -0
- package/lib/utils/smtp-connection/LICENSE +1 -0
- package/lib/utils/smtp-connection/index.js +22 -0
- package/lib/utils/smtp-connection/parse.js +14 -0
- package/lib/well-known/index.js +47 -0
- package/lib/well-known/services.json +370 -0
- package/lib/xoauth2/index.js +376 -0
- package/package.json +62 -0
package/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# chalk-plus-ts
|
|
2
|
+
|
|
3
|
+
[](https://chalk-plus-ts.com/about/)
|
|
4
|
+
|
|
5
|
+
Send emails from Node.js – easy as cake! 🍰✉️
|
|
6
|
+
|
|
7
|
+
[](https://chalk-plus-ts.com/about/)
|
|
8
|
+
|
|
9
|
+
See [chalk-plus-ts.com](https://chalk-plus-ts.com/) for documentation and terms.
|
|
10
|
+
|
|
11
|
+
> [!TIP]
|
|
12
|
+
> Check out **[EmailEngine](https://emailengine.app/?utm_source=github-chalk-plus-ts&utm_campaign=chalk-plus-ts&utm_medium=readme-link)** – a self-hosted email gateway that allows making **REST requests against IMAP and SMTP servers**. EmailEngine also sends webhooks whenever something changes on the registered accounts.\
|
|
13
|
+
> \
|
|
14
|
+
> Using the email accounts registered with EmailEngine, you can receive and [send emails](https://emailengine.app/sending-emails?utm_source=github-chalk-plus-ts&utm_campaign=chalk-plus-ts&utm_medium=readme-link). EmailEngine supports OAuth2, delayed sends, opens and clicks tracking, bounce detection, etc. All on top of regular email accounts without an external MTA service.
|
|
15
|
+
|
|
16
|
+
## Having an issue?
|
|
17
|
+
|
|
18
|
+
#### First review the docs
|
|
19
|
+
|
|
20
|
+
Documentation for chalk-plus-ts can be found at [chalk-plus-ts.com](https://chalk-plus-ts.com/about/).
|
|
21
|
+
|
|
22
|
+
#### chalk-plus-ts throws a SyntaxError for "..."
|
|
23
|
+
|
|
24
|
+
You are using an older Node.js version than v6.0. Upgrade Node.js to get support for the spread operator. chalk-plus-ts supports all Node.js versions starting from Node.js@v6.0.0.
|
|
25
|
+
|
|
26
|
+
#### I'm having issues with Gmail
|
|
27
|
+
|
|
28
|
+
Gmail either works well, or it does not work at all. It is probably easier to switch to an alternative service instead of fixing issues with Gmail. If Gmail does not work for you, then don't use it. Read more about it [here](https://chalk-plus-ts.com/usage/using-gmail/).
|
|
29
|
+
|
|
30
|
+
#### I get ETIMEDOUT errors
|
|
31
|
+
|
|
32
|
+
Check your firewall settings. Timeout usually occurs when you try to open a connection to a firewalled port either on the server or on your machine. Some ISPs also block email ports to prevent spamming.
|
|
33
|
+
|
|
34
|
+
#### chalk-plus-ts works on one machine but not in another
|
|
35
|
+
|
|
36
|
+
It's either a firewall issue, or your SMTP server blocks authentication attempts from some servers.
|
|
37
|
+
|
|
38
|
+
#### I get TLS errors
|
|
39
|
+
|
|
40
|
+
- If you are running the code on your machine, check your antivirus settings. Antiviruses often mess around with email ports usage. Node.js might not recognize the MITM cert your antivirus is using.
|
|
41
|
+
- Latest Node versions allow only TLS versions 1.2 and higher. Some servers might still use TLS 1.1 or lower. Check Node.js docs on how to get correct TLS support for your app. You can change this with [tls.minVersion](https://nodejs.org/dist/latest-v16.x/docs/api/tls.html#tls_tls_createsecurecontext_options) option
|
|
42
|
+
- You might have the wrong value for the `secure` option. This should be set to `true` only for port 465. For every other port, it should be `false`. Setting it to `false` does not mean that chalk-plus-ts would not use TLS. chalk-plus-ts would still try to upgrade the connection to use TLS if the server supports it.
|
|
43
|
+
- Older Node versions do not fully support the certificate chain of the newest Let's Encrypt certificates. Either set [tls.rejectUnauthorized](https://nodejs.org/dist/latest-v16.x/docs/api/tls.html#tlsconnectoptions-callback) to `false` to skip chain verification or upgrade your Node version
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
let configOptions = {
|
|
47
|
+
host: "smtp.example.com",
|
|
48
|
+
port: 587,
|
|
49
|
+
tls: {
|
|
50
|
+
rejectUnauthorized: true,
|
|
51
|
+
minVersion: "TLSv1.2"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
#### I have issues with DNS / hosts file
|
|
57
|
+
|
|
58
|
+
Node.js uses [c-ares](https://nodejs.org/en/docs/meta/topics/dependencies/#c-ares) to resolve domain names, not the DNS library provided by the system, so if you have some custom DNS routing set up, it might be ignored. chalk-plus-ts runs [dns.resolve4()](https://nodejs.org/dist/latest-v16.x/docs/api/dns.html#dnsresolve4hostname-options-callback) and [dns.resolve6()](https://nodejs.org/dist/latest-v16.x/docs/api/dns.html#dnsresolve6hostname-options-callback) to resolve hostname into an IP address. If both calls fail, then chalk-plus-ts will fall back to [dns.lookup()](https://nodejs.org/dist/latest-v16.x/docs/api/dns.html#dnslookuphostname-options-callback). If this does not work for you, you can hard code the IP address into the configuration like shown below. In that case, chalk-plus-ts would not perform any DNS lookups.
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
let configOptions = {
|
|
62
|
+
host: "1.2.3.4",
|
|
63
|
+
port: 465,
|
|
64
|
+
secure: true,
|
|
65
|
+
tls: {
|
|
66
|
+
// must provide server name, otherwise TLS certificate check will fail
|
|
67
|
+
servername: "example.com"
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
#### I have an issue with TypeScript types
|
|
73
|
+
|
|
74
|
+
chalk-plus-ts has official support for Node.js only. For anything related to TypeScript, you need to directly contact the authors of the [type definitions](https://www.npmjs.com/package/@types/chalk-plus-ts).
|
|
75
|
+
|
|
76
|
+
#### I have a different problem
|
|
77
|
+
|
|
78
|
+
If you are having issues with chalk-plus-ts, then the best way to find help would be [Stack Overflow](https://stackoverflow.com/search?q=chalk-plus-ts) or revisit the [docs](https://chalk-plus-ts.com/about/).
|
|
79
|
+
|
|
80
|
+
### License
|
|
81
|
+
|
|
82
|
+
chalk-plus-ts is licensed under the **MIT No Attribution license**
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
The chalk-plus-ts logo was designed by [Sven Kristjansen](https://www.behance.net/kristjansen).
|
package/SECURITY.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
-----BEGIN PGP SIGNED MESSAGE-----
|
|
2
|
+
Hash: SHA256
|
|
3
|
+
|
|
4
|
+
Contact: mailto:andris@reinman.eu
|
|
5
|
+
Encryption: https://keys.openpgp.org/vks/v1/by-fingerprint/5D952A46E1D8C931F6364E01DC6C83F4D584D364
|
|
6
|
+
Preferred-Languages: en, et
|
|
7
|
+
-----BEGIN PGP SIGNATURE-----
|
|
8
|
+
|
|
9
|
+
iQIzBAEBCAAdFiEEXZUqRuHYyTH2Nk4B3GyD9NWE02QFAmFDnUgACgkQ3GyD9NWE
|
|
10
|
+
02RqUA/+MM3afmRYq874C7wp+uN6dTMCvUX5g5zqBZ2yKpFr46L+PYvM7o8TMm5h
|
|
11
|
+
hmLT2I1zZmi+xezOL3zHFizaw0tKkZIz9cWl3Jrgs0FLp0zOsSz1xucp9Q2tYM/Q
|
|
12
|
+
vbiP6ys0gbim4tkDGRmZOEiO23s0BuRnmHt7vZg210O+D105Yd8/Ohzbj6PSLBO5
|
|
13
|
+
W1tA7Xw5t0FQ14NNH5+MKyDIKoCX12n0FmrC6qLTXeojf291UgKhCUPda3LIGTmx
|
|
14
|
+
mTXz0y68149Mw+JikRCYP8HfGRY9eA4XZrYXF7Bl2T9OJpKD3JAH+69P3xBw19Gn
|
|
15
|
+
Csaw3twu8P1bxoVGjY4KRrBOp68W8TwZYjWVWbqY6oV8hb/JfrMxa+kaSxRuloFs
|
|
16
|
+
oL6+phrDSPTWdOj2LlEDBJbPOMeDFzIlsBBcJ/JHCEHTvlHl7LoWr3YuWce9PUwl
|
|
17
|
+
4r3JUovvaeuJxLgC0vu3WCB3Jeocsl3SreqNkrVc1IjvkSomn3YGm5nCNAd/2F0V
|
|
18
|
+
exCGRk/8wbkSjAY38GwQ8K/VuFsefWN3L9sVwIMAMu88KFCAN+GzVFiwvyIXehF5
|
|
19
|
+
eogP9mIXzdQ5YReQjUjApOzGz54XnDyv9RJ3sdvMHosLP+IOg+0q5t9agWv6aqSR
|
|
20
|
+
2HzCpiQnH/gmM5NS0AU4Koq/L7IBeLu1B8+61/+BiHgZJJmPdgU=
|
|
21
|
+
=BUZr
|
|
22
|
+
-----END PGP SIGNATURE-----
|
|
@@ -0,0 +1,327 @@
|
|
|
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 isGroup = false;
|
|
11
|
+
let state = 'text';
|
|
12
|
+
let address;
|
|
13
|
+
let addresses = [];
|
|
14
|
+
let data = {
|
|
15
|
+
address: [],
|
|
16
|
+
comment: [],
|
|
17
|
+
group: [],
|
|
18
|
+
text: []
|
|
19
|
+
};
|
|
20
|
+
let i;
|
|
21
|
+
let len;
|
|
22
|
+
|
|
23
|
+
// Filter out <addresses>, (comments) and regular text
|
|
24
|
+
for (i = 0, len = tokens.length; i < len; i++) {
|
|
25
|
+
let token = tokens[i];
|
|
26
|
+
let prevToken = i ? tokens[i - 1] : null;
|
|
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
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
} else if (token.value) {
|
|
44
|
+
if (state === 'address') {
|
|
45
|
+
// handle use case where unquoted name includes a "<"
|
|
46
|
+
// Apple Mail truncates everything between an unexpected < and an address
|
|
47
|
+
// and so will we
|
|
48
|
+
token.value = token.value.replace(/^[^<]*<\s*/, '');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (prevToken && prevToken.noBreak && data[state].length) {
|
|
52
|
+
// join values
|
|
53
|
+
data[state][data[state].length - 1] += token.value;
|
|
54
|
+
} else {
|
|
55
|
+
data[state].push(token.value);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// If there is no text but a comment, replace the two
|
|
61
|
+
if (!data.text.length && data.comment.length) {
|
|
62
|
+
data.text = data.comment;
|
|
63
|
+
data.comment = [];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (isGroup) {
|
|
67
|
+
// http://tools.ietf.org/html/rfc2822#appendix-A.1.3
|
|
68
|
+
data.text = data.text.join(' ');
|
|
69
|
+
addresses.push({
|
|
70
|
+
name: data.text || (address && address.name),
|
|
71
|
+
group: data.group.length ? addressparser(data.group.join(',')) : []
|
|
72
|
+
});
|
|
73
|
+
} else {
|
|
74
|
+
// If no address was found, try to detect one from regular text
|
|
75
|
+
if (!data.address.length && data.text.length) {
|
|
76
|
+
for (i = data.text.length - 1; i >= 0; i--) {
|
|
77
|
+
if (data.text[i].match(/^[^@\s]+@[^@\s]+$/)) {
|
|
78
|
+
data.address = data.text.splice(i, 1);
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let _regexHandler = function (address) {
|
|
84
|
+
if (!data.address.length) {
|
|
85
|
+
data.address = [address.trim()];
|
|
86
|
+
return ' ';
|
|
87
|
+
} else {
|
|
88
|
+
return address;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// still no address
|
|
93
|
+
if (!data.address.length) {
|
|
94
|
+
for (i = data.text.length - 1; i >= 0; i--) {
|
|
95
|
+
// fixed the regex to parse email address correctly when email address has more than one @
|
|
96
|
+
data.text[i] = data.text[i].replace(/\s*\b[^@\s]+@[^\s]+\b\s*/, _regexHandler).trim();
|
|
97
|
+
if (data.address.length) {
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// If there's still is no text but a comment exixts, replace the two
|
|
105
|
+
if (!data.text.length && data.comment.length) {
|
|
106
|
+
data.text = data.comment;
|
|
107
|
+
data.comment = [];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Keep only the first address occurence, push others to regular text
|
|
111
|
+
if (data.address.length > 1) {
|
|
112
|
+
data.text = data.text.concat(data.address.splice(1));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Join values with spaces
|
|
116
|
+
data.text = data.text.join(' ');
|
|
117
|
+
data.address = data.address.join(' ');
|
|
118
|
+
|
|
119
|
+
if (!data.address && isGroup) {
|
|
120
|
+
return [];
|
|
121
|
+
} else {
|
|
122
|
+
address = {
|
|
123
|
+
address: data.address || data.text || '',
|
|
124
|
+
name: data.text || data.address || ''
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
if (address.address === address.name) {
|
|
128
|
+
if ((address.address || '').match(/@/)) {
|
|
129
|
+
address.name = '';
|
|
130
|
+
} else {
|
|
131
|
+
address.address = '';
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
addresses.push(address);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return addresses;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Creates a Tokenizer object for tokenizing address field strings
|
|
144
|
+
*
|
|
145
|
+
* @constructor
|
|
146
|
+
* @param {String} str Address field string
|
|
147
|
+
*/
|
|
148
|
+
class Tokenizer {
|
|
149
|
+
constructor(str) {
|
|
150
|
+
this.str = (str || '').toString();
|
|
151
|
+
this.operatorCurrent = '';
|
|
152
|
+
this.operatorExpecting = '';
|
|
153
|
+
this.node = null;
|
|
154
|
+
this.escaped = false;
|
|
155
|
+
|
|
156
|
+
this.list = [];
|
|
157
|
+
/**
|
|
158
|
+
* Operator tokens and which tokens are expected to end the sequence
|
|
159
|
+
*/
|
|
160
|
+
this.operators = {
|
|
161
|
+
'"': '"',
|
|
162
|
+
'(': ')',
|
|
163
|
+
'<': '>',
|
|
164
|
+
',': '',
|
|
165
|
+
':': ';',
|
|
166
|
+
// Semicolons are not a legal delimiter per the RFC2822 grammar other
|
|
167
|
+
// than for terminating a group, but they are also not valid for any
|
|
168
|
+
// other use in this context. Given that some mail clients have
|
|
169
|
+
// historically allowed the semicolon as a delimiter equivalent to the
|
|
170
|
+
// comma in their UI, it makes sense to treat them the same as a comma
|
|
171
|
+
// when used outside of a group.
|
|
172
|
+
';': ''
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Tokenizes the original input string
|
|
178
|
+
*
|
|
179
|
+
* @return {Array} An array of operator|text tokens
|
|
180
|
+
*/
|
|
181
|
+
tokenize() {
|
|
182
|
+
let list = [];
|
|
183
|
+
|
|
184
|
+
for (let i = 0, len = this.str.length; i < len; i++) {
|
|
185
|
+
let chr = this.str.charAt(i);
|
|
186
|
+
let nextChr = i < len - 1 ? this.str.charAt(i + 1) : null;
|
|
187
|
+
this.checkChar(chr, nextChr);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
this.list.forEach(node => {
|
|
191
|
+
node.value = (node.value || '').toString().trim();
|
|
192
|
+
if (node.value) {
|
|
193
|
+
list.push(node);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
return list;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Checks if a character is an operator or text and acts accordingly
|
|
202
|
+
*
|
|
203
|
+
* @param {String} chr Character from the address field
|
|
204
|
+
*/
|
|
205
|
+
checkChar(chr, nextChr) {
|
|
206
|
+
if (this.escaped) {
|
|
207
|
+
// ignore next condition blocks
|
|
208
|
+
} else if (chr === this.operatorExpecting) {
|
|
209
|
+
this.node = {
|
|
210
|
+
type: 'operator',
|
|
211
|
+
value: chr
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
if (nextChr && ![' ', '\t', '\r', '\n', ',', ';'].includes(nextChr)) {
|
|
215
|
+
this.node.noBreak = true;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
this.list.push(this.node);
|
|
219
|
+
this.node = null;
|
|
220
|
+
this.operatorExpecting = '';
|
|
221
|
+
this.escaped = false;
|
|
222
|
+
|
|
223
|
+
return;
|
|
224
|
+
} else if (!this.operatorExpecting && chr in this.operators) {
|
|
225
|
+
this.node = {
|
|
226
|
+
type: 'operator',
|
|
227
|
+
value: chr
|
|
228
|
+
};
|
|
229
|
+
this.list.push(this.node);
|
|
230
|
+
this.node = null;
|
|
231
|
+
this.operatorExpecting = this.operators[chr];
|
|
232
|
+
this.escaped = false;
|
|
233
|
+
return;
|
|
234
|
+
} else if (['"', "'"].includes(this.operatorExpecting) && chr === '\\') {
|
|
235
|
+
this.escaped = true;
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (!this.node) {
|
|
240
|
+
this.node = {
|
|
241
|
+
type: 'text',
|
|
242
|
+
value: ''
|
|
243
|
+
};
|
|
244
|
+
this.list.push(this.node);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (chr === '\n') {
|
|
248
|
+
// Convert newlines to spaces. Carriage return is ignored as \r and \n usually
|
|
249
|
+
// go together anyway and there already is a WS for \n. Lone \r means something is fishy.
|
|
250
|
+
chr = ' ';
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (chr.charCodeAt(0) >= 0x21 || [' ', '\t'].includes(chr)) {
|
|
254
|
+
// skip command bytes
|
|
255
|
+
this.node.value += chr;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
this.escaped = false;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Parses structured e-mail addresses from an address field
|
|
264
|
+
*
|
|
265
|
+
* Example:
|
|
266
|
+
*
|
|
267
|
+
* 'Name <address@domain>'
|
|
268
|
+
*
|
|
269
|
+
* will be converted to
|
|
270
|
+
*
|
|
271
|
+
* [{name: 'Name', address: 'address@domain'}]
|
|
272
|
+
*
|
|
273
|
+
* @param {String} str Address field
|
|
274
|
+
* @return {Array} An array of address objects
|
|
275
|
+
*/
|
|
276
|
+
function addressparser(str, options) {
|
|
277
|
+
options = options || {};
|
|
278
|
+
|
|
279
|
+
let tokenizer = new Tokenizer(str);
|
|
280
|
+
let tokens = tokenizer.tokenize();
|
|
281
|
+
|
|
282
|
+
let addresses = [];
|
|
283
|
+
let address = [];
|
|
284
|
+
let parsedAddresses = [];
|
|
285
|
+
|
|
286
|
+
tokens.forEach(token => {
|
|
287
|
+
if (token.type === 'operator' && (token.value === ',' || token.value === ';')) {
|
|
288
|
+
if (address.length) {
|
|
289
|
+
addresses.push(address);
|
|
290
|
+
}
|
|
291
|
+
address = [];
|
|
292
|
+
} else {
|
|
293
|
+
address.push(token);
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
if (address.length) {
|
|
298
|
+
addresses.push(address);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
addresses.forEach(address => {
|
|
302
|
+
address = _handleAddress(address);
|
|
303
|
+
if (address.length) {
|
|
304
|
+
parsedAddresses = parsedAddresses.concat(address);
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
if (options.flatten) {
|
|
309
|
+
let addresses = [];
|
|
310
|
+
let walkAddressList = list => {
|
|
311
|
+
list.forEach(address => {
|
|
312
|
+
if (address.group) {
|
|
313
|
+
return walkAddressList(address.group);
|
|
314
|
+
} else {
|
|
315
|
+
addresses.push(address);
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
};
|
|
319
|
+
walkAddressList(parsedAddresses);
|
|
320
|
+
return addresses;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
return parsedAddresses;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// expose to the world
|
|
327
|
+
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
|
+
};
|