@nocobase/plugin-notifications 0.14.0-alpha.7 → 0.15.0-alpha.1
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/dist/externalVersion.js +3 -3
- package/dist/node_modules/nodemailer/.gitattributes +6 -0
- package/dist/node_modules/nodemailer/.ncurc.js +7 -0
- package/dist/node_modules/nodemailer/.prettierrc.js +8 -0
- package/dist/node_modules/nodemailer/LICENSE +16 -0
- package/dist/node_modules/nodemailer/SECURITY.txt +22 -0
- package/dist/node_modules/nodemailer/lib/addressparser/index.js +313 -0
- package/dist/node_modules/nodemailer/lib/base64/index.js +142 -0
- package/dist/node_modules/nodemailer/lib/dkim/index.js +251 -0
- package/dist/node_modules/nodemailer/lib/dkim/message-parser.js +155 -0
- package/dist/node_modules/nodemailer/lib/dkim/relaxed-body.js +154 -0
- package/dist/node_modules/nodemailer/lib/dkim/sign.js +117 -0
- package/dist/node_modules/nodemailer/lib/fetch/cookies.js +281 -0
- package/dist/node_modules/nodemailer/lib/fetch/index.js +274 -0
- package/dist/node_modules/nodemailer/lib/json-transport/index.js +82 -0
- package/dist/node_modules/nodemailer/lib/mail-composer/index.js +558 -0
- package/dist/node_modules/nodemailer/lib/mailer/index.js +427 -0
- package/dist/node_modules/nodemailer/lib/mailer/mail-message.js +315 -0
- package/dist/node_modules/nodemailer/lib/mime-funcs/index.js +625 -0
- package/dist/node_modules/nodemailer/lib/mime-funcs/mime-types.js +2102 -0
- package/dist/node_modules/nodemailer/lib/mime-node/index.js +1305 -0
- package/dist/node_modules/nodemailer/lib/mime-node/last-newline.js +33 -0
- package/dist/node_modules/nodemailer/lib/mime-node/le-unix.js +43 -0
- package/dist/node_modules/nodemailer/lib/mime-node/le-windows.js +52 -0
- package/dist/node_modules/nodemailer/lib/nodemailer.js +1 -0
- package/dist/node_modules/nodemailer/lib/qp/index.js +219 -0
- package/dist/node_modules/nodemailer/lib/sendmail-transport/index.js +210 -0
- package/dist/node_modules/nodemailer/lib/ses-transport/index.js +349 -0
- package/dist/node_modules/nodemailer/lib/shared/index.js +638 -0
- package/dist/node_modules/nodemailer/lib/smtp-connection/data-stream.js +108 -0
- package/dist/node_modules/nodemailer/lib/smtp-connection/http-proxy-client.js +143 -0
- package/dist/node_modules/nodemailer/lib/smtp-connection/index.js +1812 -0
- package/dist/node_modules/nodemailer/lib/smtp-pool/index.js +648 -0
- package/dist/node_modules/nodemailer/lib/smtp-pool/pool-resource.js +253 -0
- package/dist/node_modules/nodemailer/lib/smtp-transport/index.js +416 -0
- package/dist/node_modules/nodemailer/lib/stream-transport/index.js +135 -0
- package/dist/node_modules/nodemailer/lib/well-known/index.js +47 -0
- package/dist/node_modules/nodemailer/lib/well-known/services.json +294 -0
- package/dist/node_modules/nodemailer/lib/xoauth2/index.js +376 -0
- package/dist/node_modules/nodemailer/package.json +1 -0
- package/dist/node_modules/nodemailer/postinstall.js +101 -0
- package/package.json +2 -2
package/dist/externalVersion.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
module.exports = {
|
|
2
|
-
"@nocobase/client": "0.
|
|
3
|
-
"@nocobase/server": "0.
|
|
4
|
-
"@nocobase/database": "0.
|
|
2
|
+
"@nocobase/client": "0.15.0-alpha.1",
|
|
3
|
+
"@nocobase/server": "0.15.0-alpha.1",
|
|
4
|
+
"@nocobase/database": "0.15.0-alpha.1",
|
|
5
5
|
"lodash": "4.17.21"
|
|
6
6
|
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Copyright (c) 2011-2019 Andris Reinman
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
11
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
12
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
13
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
14
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
15
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
16
|
+
SOFTWARE.
|
|
@@ -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,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
|
+
};
|