@nocobase/plugin-notification-email 1.4.0-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +159 -0
- package/README.md +1 -0
- package/client.d.ts +2 -0
- package/client.js +1 -0
- package/dist/client/ConfigForm.d.ts +10 -0
- package/dist/client/MessageConfigForm.d.ts +12 -0
- package/dist/client/hooks/useTranslation.d.ts +9 -0
- package/dist/client/index.d.ts +15 -0
- package/dist/client/index.js +16 -0
- package/dist/constant.d.ts +10 -0
- package/dist/constant.js +39 -0
- package/dist/externalVersion.js +17 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +48 -0
- package/dist/locale/en-US.json +22 -0
- package/dist/locale/zh-CN.json +22 -0
- 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 +565 -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 +338 -0
- package/dist/node_modules/nodemailer/lib/xoauth2/index.js +376 -0
- package/dist/node_modules/nodemailer/package.json +1 -0
- package/dist/server/index.d.ts +9 -0
- package/dist/server/index.js +42 -0
- package/dist/server/mail-server.d.ts +14 -0
- package/dist/server/mail-server.js +78 -0
- package/dist/server/plugin.d.ts +19 -0
- package/dist/server/plugin.js +69 -0
- package/package.json +23 -0
- package/server.d.ts +2 -0
- package/server.js +1 -0
- package/tsconfig.json +7 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Transform = require('stream').Transform;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* MessageParser instance is a transform stream that separates message headers
|
|
7
|
+
* from the rest of the body. Headers are emitted with the 'headers' event. Message
|
|
8
|
+
* body is passed on as the resulting stream.
|
|
9
|
+
*/
|
|
10
|
+
class MessageParser extends Transform {
|
|
11
|
+
constructor(options) {
|
|
12
|
+
super(options);
|
|
13
|
+
this.lastBytes = Buffer.alloc(4);
|
|
14
|
+
this.headersParsed = false;
|
|
15
|
+
this.headerBytes = 0;
|
|
16
|
+
this.headerChunks = [];
|
|
17
|
+
this.rawHeaders = false;
|
|
18
|
+
this.bodySize = 0;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Keeps count of the last 4 bytes in order to detect line breaks on chunk boundaries
|
|
23
|
+
*
|
|
24
|
+
* @param {Buffer} data Next data chunk from the stream
|
|
25
|
+
*/
|
|
26
|
+
updateLastBytes(data) {
|
|
27
|
+
let lblen = this.lastBytes.length;
|
|
28
|
+
let nblen = Math.min(data.length, lblen);
|
|
29
|
+
|
|
30
|
+
// shift existing bytes
|
|
31
|
+
for (let i = 0, len = lblen - nblen; i < len; i++) {
|
|
32
|
+
this.lastBytes[i] = this.lastBytes[i + nblen];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// add new bytes
|
|
36
|
+
for (let i = 1; i <= nblen; i++) {
|
|
37
|
+
this.lastBytes[lblen - i] = data[data.length - i];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Finds and removes message headers from the remaining body. We want to keep
|
|
43
|
+
* headers separated until final delivery to be able to modify these
|
|
44
|
+
*
|
|
45
|
+
* @param {Buffer} data Next chunk of data
|
|
46
|
+
* @return {Boolean} Returns true if headers are already found or false otherwise
|
|
47
|
+
*/
|
|
48
|
+
checkHeaders(data) {
|
|
49
|
+
if (this.headersParsed) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let lblen = this.lastBytes.length;
|
|
54
|
+
let headerPos = 0;
|
|
55
|
+
this.curLinePos = 0;
|
|
56
|
+
for (let i = 0, len = this.lastBytes.length + data.length; i < len; i++) {
|
|
57
|
+
let chr;
|
|
58
|
+
if (i < lblen) {
|
|
59
|
+
chr = this.lastBytes[i];
|
|
60
|
+
} else {
|
|
61
|
+
chr = data[i - lblen];
|
|
62
|
+
}
|
|
63
|
+
if (chr === 0x0a && i) {
|
|
64
|
+
let pr1 = i - 1 < lblen ? this.lastBytes[i - 1] : data[i - 1 - lblen];
|
|
65
|
+
let pr2 = i > 1 ? (i - 2 < lblen ? this.lastBytes[i - 2] : data[i - 2 - lblen]) : false;
|
|
66
|
+
if (pr1 === 0x0a) {
|
|
67
|
+
this.headersParsed = true;
|
|
68
|
+
headerPos = i - lblen + 1;
|
|
69
|
+
this.headerBytes += headerPos;
|
|
70
|
+
break;
|
|
71
|
+
} else if (pr1 === 0x0d && pr2 === 0x0a) {
|
|
72
|
+
this.headersParsed = true;
|
|
73
|
+
headerPos = i - lblen + 1;
|
|
74
|
+
this.headerBytes += headerPos;
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (this.headersParsed) {
|
|
81
|
+
this.headerChunks.push(data.slice(0, headerPos));
|
|
82
|
+
this.rawHeaders = Buffer.concat(this.headerChunks, this.headerBytes);
|
|
83
|
+
this.headerChunks = null;
|
|
84
|
+
this.emit('headers', this.parseHeaders());
|
|
85
|
+
if (data.length - 1 > headerPos) {
|
|
86
|
+
let chunk = data.slice(headerPos);
|
|
87
|
+
this.bodySize += chunk.length;
|
|
88
|
+
// this would be the first chunk of data sent downstream
|
|
89
|
+
setImmediate(() => this.push(chunk));
|
|
90
|
+
}
|
|
91
|
+
return false;
|
|
92
|
+
} else {
|
|
93
|
+
this.headerBytes += data.length;
|
|
94
|
+
this.headerChunks.push(data);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// store last 4 bytes to catch header break
|
|
98
|
+
this.updateLastBytes(data);
|
|
99
|
+
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
_transform(chunk, encoding, callback) {
|
|
104
|
+
if (!chunk || !chunk.length) {
|
|
105
|
+
return callback();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (typeof chunk === 'string') {
|
|
109
|
+
chunk = Buffer.from(chunk, encoding);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
let headersFound;
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
headersFound = this.checkHeaders(chunk);
|
|
116
|
+
} catch (E) {
|
|
117
|
+
return callback(E);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (headersFound) {
|
|
121
|
+
this.bodySize += chunk.length;
|
|
122
|
+
this.push(chunk);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
setImmediate(callback);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
_flush(callback) {
|
|
129
|
+
if (this.headerChunks) {
|
|
130
|
+
let chunk = Buffer.concat(this.headerChunks, this.headerBytes);
|
|
131
|
+
this.bodySize += chunk.length;
|
|
132
|
+
this.push(chunk);
|
|
133
|
+
this.headerChunks = null;
|
|
134
|
+
}
|
|
135
|
+
callback();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
parseHeaders() {
|
|
139
|
+
let lines = (this.rawHeaders || '').toString().split(/\r?\n/);
|
|
140
|
+
for (let i = lines.length - 1; i > 0; i--) {
|
|
141
|
+
if (/^\s/.test(lines[i])) {
|
|
142
|
+
lines[i - 1] += '\n' + lines[i];
|
|
143
|
+
lines.splice(i, 1);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return lines
|
|
147
|
+
.filter(line => line.trim())
|
|
148
|
+
.map(line => ({
|
|
149
|
+
key: line.substr(0, line.indexOf(':')).trim().toLowerCase(),
|
|
150
|
+
line
|
|
151
|
+
}));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
module.exports = MessageParser;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// streams through a message body and calculates relaxed body hash
|
|
4
|
+
|
|
5
|
+
const Transform = require('stream').Transform;
|
|
6
|
+
const crypto = require('crypto');
|
|
7
|
+
|
|
8
|
+
class RelaxedBody extends Transform {
|
|
9
|
+
constructor(options) {
|
|
10
|
+
super();
|
|
11
|
+
options = options || {};
|
|
12
|
+
this.chunkBuffer = [];
|
|
13
|
+
this.chunkBufferLen = 0;
|
|
14
|
+
this.bodyHash = crypto.createHash(options.hashAlgo || 'sha1');
|
|
15
|
+
this.remainder = '';
|
|
16
|
+
this.byteLength = 0;
|
|
17
|
+
|
|
18
|
+
this.debug = options.debug;
|
|
19
|
+
this._debugBody = options.debug ? [] : false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
updateHash(chunk) {
|
|
23
|
+
let bodyStr;
|
|
24
|
+
|
|
25
|
+
// find next remainder
|
|
26
|
+
let nextRemainder = '';
|
|
27
|
+
|
|
28
|
+
// This crux finds and removes the spaces from the last line and the newline characters after the last non-empty line
|
|
29
|
+
// If we get another chunk that does not match this description then we can restore the previously processed data
|
|
30
|
+
let state = 'file';
|
|
31
|
+
for (let i = chunk.length - 1; i >= 0; i--) {
|
|
32
|
+
let c = chunk[i];
|
|
33
|
+
|
|
34
|
+
if (state === 'file' && (c === 0x0a || c === 0x0d)) {
|
|
35
|
+
// do nothing, found \n or \r at the end of chunk, stil end of file
|
|
36
|
+
} else if (state === 'file' && (c === 0x09 || c === 0x20)) {
|
|
37
|
+
// switch to line ending mode, this is the last non-empty line
|
|
38
|
+
state = 'line';
|
|
39
|
+
} else if (state === 'line' && (c === 0x09 || c === 0x20)) {
|
|
40
|
+
// do nothing, found ' ' or \t at the end of line, keep processing the last non-empty line
|
|
41
|
+
} else if (state === 'file' || state === 'line') {
|
|
42
|
+
// non line/file ending character found, switch to body mode
|
|
43
|
+
state = 'body';
|
|
44
|
+
if (i === chunk.length - 1) {
|
|
45
|
+
// final char is not part of line end or file end, so do nothing
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (i === 0) {
|
|
51
|
+
// reached to the beginning of the chunk, check if it is still about the ending
|
|
52
|
+
// and if the remainder also matches
|
|
53
|
+
if (
|
|
54
|
+
(state === 'file' && (!this.remainder || /[\r\n]$/.test(this.remainder))) ||
|
|
55
|
+
(state === 'line' && (!this.remainder || /[ \t]$/.test(this.remainder)))
|
|
56
|
+
) {
|
|
57
|
+
// keep everything
|
|
58
|
+
this.remainder += chunk.toString('binary');
|
|
59
|
+
return;
|
|
60
|
+
} else if (state === 'line' || state === 'file') {
|
|
61
|
+
// process existing remainder as normal line but store the current chunk
|
|
62
|
+
nextRemainder = chunk.toString('binary');
|
|
63
|
+
chunk = false;
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (state !== 'body') {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// reached first non ending byte
|
|
73
|
+
nextRemainder = chunk.slice(i + 1).toString('binary');
|
|
74
|
+
chunk = chunk.slice(0, i + 1);
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let needsFixing = !!this.remainder;
|
|
79
|
+
if (chunk && !needsFixing) {
|
|
80
|
+
// check if we even need to change anything
|
|
81
|
+
for (let i = 0, len = chunk.length; i < len; i++) {
|
|
82
|
+
if (i && chunk[i] === 0x0a && chunk[i - 1] !== 0x0d) {
|
|
83
|
+
// missing \r before \n
|
|
84
|
+
needsFixing = true;
|
|
85
|
+
break;
|
|
86
|
+
} else if (i && chunk[i] === 0x0d && chunk[i - 1] === 0x20) {
|
|
87
|
+
// trailing WSP found
|
|
88
|
+
needsFixing = true;
|
|
89
|
+
break;
|
|
90
|
+
} else if (i && chunk[i] === 0x20 && chunk[i - 1] === 0x20) {
|
|
91
|
+
// multiple spaces found, needs to be replaced with just one
|
|
92
|
+
needsFixing = true;
|
|
93
|
+
break;
|
|
94
|
+
} else if (chunk[i] === 0x09) {
|
|
95
|
+
// TAB found, needs to be replaced with a space
|
|
96
|
+
needsFixing = true;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (needsFixing) {
|
|
103
|
+
bodyStr = this.remainder + (chunk ? chunk.toString('binary') : '');
|
|
104
|
+
this.remainder = nextRemainder;
|
|
105
|
+
bodyStr = bodyStr
|
|
106
|
+
.replace(/\r?\n/g, '\n') // use js line endings
|
|
107
|
+
.replace(/[ \t]*$/gm, '') // remove line endings, rtrim
|
|
108
|
+
.replace(/[ \t]+/gm, ' ') // single spaces
|
|
109
|
+
.replace(/\n/g, '\r\n'); // restore rfc822 line endings
|
|
110
|
+
chunk = Buffer.from(bodyStr, 'binary');
|
|
111
|
+
} else if (nextRemainder) {
|
|
112
|
+
this.remainder = nextRemainder;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (this.debug) {
|
|
116
|
+
this._debugBody.push(chunk);
|
|
117
|
+
}
|
|
118
|
+
this.bodyHash.update(chunk);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
_transform(chunk, encoding, callback) {
|
|
122
|
+
if (!chunk || !chunk.length) {
|
|
123
|
+
return callback();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (typeof chunk === 'string') {
|
|
127
|
+
chunk = Buffer.from(chunk, encoding);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
this.updateHash(chunk);
|
|
131
|
+
|
|
132
|
+
this.byteLength += chunk.length;
|
|
133
|
+
this.push(chunk);
|
|
134
|
+
callback();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
_flush(callback) {
|
|
138
|
+
// generate final hash and emit it
|
|
139
|
+
if (/[\r\n]$/.test(this.remainder) && this.byteLength > 2) {
|
|
140
|
+
// add terminating line end
|
|
141
|
+
this.bodyHash.update(Buffer.from('\r\n'));
|
|
142
|
+
}
|
|
143
|
+
if (!this.byteLength) {
|
|
144
|
+
// emit empty line buffer to keep the stream flowing
|
|
145
|
+
this.push(Buffer.from('\r\n'));
|
|
146
|
+
// this.bodyHash.update(Buffer.from('\r\n'));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
this.emit('hash', this.bodyHash.digest('base64'), this.debug ? Buffer.concat(this._debugBody) : false);
|
|
150
|
+
callback();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
module.exports = RelaxedBody;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const punycode = require('punycode');
|
|
4
|
+
const mimeFuncs = require('../mime-funcs');
|
|
5
|
+
const crypto = require('crypto');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Returns DKIM signature header line
|
|
9
|
+
*
|
|
10
|
+
* @param {Object} headers Parsed headers object from MessageParser
|
|
11
|
+
* @param {String} bodyHash Base64 encoded hash of the message
|
|
12
|
+
* @param {Object} options DKIM options
|
|
13
|
+
* @param {String} options.domainName Domain name to be signed for
|
|
14
|
+
* @param {String} options.keySelector DKIM key selector to use
|
|
15
|
+
* @param {String} options.privateKey DKIM private key to use
|
|
16
|
+
* @return {String} Complete header line
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
module.exports = (headers, hashAlgo, bodyHash, options) => {
|
|
20
|
+
options = options || {};
|
|
21
|
+
|
|
22
|
+
// all listed fields from RFC4871 #5.5
|
|
23
|
+
let defaultFieldNames =
|
|
24
|
+
'From:Sender:Reply-To:Subject:Date:Message-ID:To:' +
|
|
25
|
+
'Cc:MIME-Version:Content-Type:Content-Transfer-Encoding:Content-ID:' +
|
|
26
|
+
'Content-Description:Resent-Date:Resent-From:Resent-Sender:' +
|
|
27
|
+
'Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:' +
|
|
28
|
+
'List-Id:List-Help:List-Unsubscribe:List-Subscribe:List-Post:' +
|
|
29
|
+
'List-Owner:List-Archive';
|
|
30
|
+
|
|
31
|
+
let fieldNames = options.headerFieldNames || defaultFieldNames;
|
|
32
|
+
|
|
33
|
+
let canonicalizedHeaderData = relaxedHeaders(headers, fieldNames, options.skipFields);
|
|
34
|
+
let dkimHeader = generateDKIMHeader(options.domainName, options.keySelector, canonicalizedHeaderData.fieldNames, hashAlgo, bodyHash);
|
|
35
|
+
|
|
36
|
+
let signer, signature;
|
|
37
|
+
|
|
38
|
+
canonicalizedHeaderData.headers += 'dkim-signature:' + relaxedHeaderLine(dkimHeader);
|
|
39
|
+
|
|
40
|
+
signer = crypto.createSign(('rsa-' + hashAlgo).toUpperCase());
|
|
41
|
+
signer.update(canonicalizedHeaderData.headers);
|
|
42
|
+
try {
|
|
43
|
+
signature = signer.sign(options.privateKey, 'base64');
|
|
44
|
+
} catch (E) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return dkimHeader + signature.replace(/(^.{73}|.{75}(?!\r?\n|\r))/g, '$&\r\n ').trim();
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
module.exports.relaxedHeaders = relaxedHeaders;
|
|
52
|
+
|
|
53
|
+
function generateDKIMHeader(domainName, keySelector, fieldNames, hashAlgo, bodyHash) {
|
|
54
|
+
let dkim = [
|
|
55
|
+
'v=1',
|
|
56
|
+
'a=rsa-' + hashAlgo,
|
|
57
|
+
'c=relaxed/relaxed',
|
|
58
|
+
'd=' + punycode.toASCII(domainName),
|
|
59
|
+
'q=dns/txt',
|
|
60
|
+
's=' + keySelector,
|
|
61
|
+
'bh=' + bodyHash,
|
|
62
|
+
'h=' + fieldNames
|
|
63
|
+
].join('; ');
|
|
64
|
+
|
|
65
|
+
return mimeFuncs.foldLines('DKIM-Signature: ' + dkim, 76) + ';\r\n b=';
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function relaxedHeaders(headers, fieldNames, skipFields) {
|
|
69
|
+
let includedFields = new Set();
|
|
70
|
+
let skip = new Set();
|
|
71
|
+
let headerFields = new Map();
|
|
72
|
+
|
|
73
|
+
(skipFields || '')
|
|
74
|
+
.toLowerCase()
|
|
75
|
+
.split(':')
|
|
76
|
+
.forEach(field => {
|
|
77
|
+
skip.add(field.trim());
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
(fieldNames || '')
|
|
81
|
+
.toLowerCase()
|
|
82
|
+
.split(':')
|
|
83
|
+
.filter(field => !skip.has(field.trim()))
|
|
84
|
+
.forEach(field => {
|
|
85
|
+
includedFields.add(field.trim());
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
for (let i = headers.length - 1; i >= 0; i--) {
|
|
89
|
+
let line = headers[i];
|
|
90
|
+
// only include the first value from bottom to top
|
|
91
|
+
if (includedFields.has(line.key) && !headerFields.has(line.key)) {
|
|
92
|
+
headerFields.set(line.key, relaxedHeaderLine(line.line));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
let headersList = [];
|
|
97
|
+
let fields = [];
|
|
98
|
+
includedFields.forEach(field => {
|
|
99
|
+
if (headerFields.has(field)) {
|
|
100
|
+
fields.push(field);
|
|
101
|
+
headersList.push(field + ':' + headerFields.get(field));
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
headers: headersList.join('\r\n') + '\r\n',
|
|
107
|
+
fieldNames: fields.join(':')
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function relaxedHeaderLine(line) {
|
|
112
|
+
return line
|
|
113
|
+
.substr(line.indexOf(':') + 1)
|
|
114
|
+
.replace(/\r?\n/g, '')
|
|
115
|
+
.replace(/\s+/g, ' ')
|
|
116
|
+
.trim();
|
|
117
|
+
}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// module to handle cookies
|
|
4
|
+
|
|
5
|
+
const urllib = require('url');
|
|
6
|
+
|
|
7
|
+
const SESSION_TIMEOUT = 1800; // 30 min
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Creates a biskviit cookie jar for managing cookie values in memory
|
|
11
|
+
*
|
|
12
|
+
* @constructor
|
|
13
|
+
* @param {Object} [options] Optional options object
|
|
14
|
+
*/
|
|
15
|
+
class Cookies {
|
|
16
|
+
constructor(options) {
|
|
17
|
+
this.options = options || {};
|
|
18
|
+
this.cookies = [];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Stores a cookie string to the cookie storage
|
|
23
|
+
*
|
|
24
|
+
* @param {String} cookieStr Value from the 'Set-Cookie:' header
|
|
25
|
+
* @param {String} url Current URL
|
|
26
|
+
*/
|
|
27
|
+
set(cookieStr, url) {
|
|
28
|
+
let urlparts = urllib.parse(url || '');
|
|
29
|
+
let cookie = this.parse(cookieStr);
|
|
30
|
+
let domain;
|
|
31
|
+
|
|
32
|
+
if (cookie.domain) {
|
|
33
|
+
domain = cookie.domain.replace(/^\./, '');
|
|
34
|
+
|
|
35
|
+
// do not allow cross origin cookies
|
|
36
|
+
if (
|
|
37
|
+
// can't be valid if the requested domain is shorter than current hostname
|
|
38
|
+
urlparts.hostname.length < domain.length ||
|
|
39
|
+
// prefix domains with dot to be sure that partial matches are not used
|
|
40
|
+
('.' + urlparts.hostname).substr(-domain.length + 1) !== '.' + domain
|
|
41
|
+
) {
|
|
42
|
+
cookie.domain = urlparts.hostname;
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
cookie.domain = urlparts.hostname;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (!cookie.path) {
|
|
49
|
+
cookie.path = this.getPath(urlparts.pathname);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// if no expire date, then use sessionTimeout value
|
|
53
|
+
if (!cookie.expires) {
|
|
54
|
+
cookie.expires = new Date(Date.now() + (Number(this.options.sessionTimeout || SESSION_TIMEOUT) || SESSION_TIMEOUT) * 1000);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return this.add(cookie);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Returns cookie string for the 'Cookie:' header.
|
|
62
|
+
*
|
|
63
|
+
* @param {String} url URL to check for
|
|
64
|
+
* @returns {String} Cookie header or empty string if no matches were found
|
|
65
|
+
*/
|
|
66
|
+
get(url) {
|
|
67
|
+
return this.list(url)
|
|
68
|
+
.map(cookie => cookie.name + '=' + cookie.value)
|
|
69
|
+
.join('; ');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Lists all valied cookie objects for the specified URL
|
|
74
|
+
*
|
|
75
|
+
* @param {String} url URL to check for
|
|
76
|
+
* @returns {Array} An array of cookie objects
|
|
77
|
+
*/
|
|
78
|
+
list(url) {
|
|
79
|
+
let result = [];
|
|
80
|
+
let i;
|
|
81
|
+
let cookie;
|
|
82
|
+
|
|
83
|
+
for (i = this.cookies.length - 1; i >= 0; i--) {
|
|
84
|
+
cookie = this.cookies[i];
|
|
85
|
+
|
|
86
|
+
if (this.isExpired(cookie)) {
|
|
87
|
+
this.cookies.splice(i, i);
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (this.match(cookie, url)) {
|
|
92
|
+
result.unshift(cookie);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Parses cookie string from the 'Set-Cookie:' header
|
|
101
|
+
*
|
|
102
|
+
* @param {String} cookieStr String from the 'Set-Cookie:' header
|
|
103
|
+
* @returns {Object} Cookie object
|
|
104
|
+
*/
|
|
105
|
+
parse(cookieStr) {
|
|
106
|
+
let cookie = {};
|
|
107
|
+
|
|
108
|
+
(cookieStr || '')
|
|
109
|
+
.toString()
|
|
110
|
+
.split(';')
|
|
111
|
+
.forEach(cookiePart => {
|
|
112
|
+
let valueParts = cookiePart.split('=');
|
|
113
|
+
let key = valueParts.shift().trim().toLowerCase();
|
|
114
|
+
let value = valueParts.join('=').trim();
|
|
115
|
+
let domain;
|
|
116
|
+
|
|
117
|
+
if (!key) {
|
|
118
|
+
// skip empty parts
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
switch (key) {
|
|
123
|
+
case 'expires':
|
|
124
|
+
value = new Date(value);
|
|
125
|
+
// ignore date if can not parse it
|
|
126
|
+
if (value.toString() !== 'Invalid Date') {
|
|
127
|
+
cookie.expires = value;
|
|
128
|
+
}
|
|
129
|
+
break;
|
|
130
|
+
|
|
131
|
+
case 'path':
|
|
132
|
+
cookie.path = value;
|
|
133
|
+
break;
|
|
134
|
+
|
|
135
|
+
case 'domain':
|
|
136
|
+
domain = value.toLowerCase();
|
|
137
|
+
if (domain.length && domain.charAt(0) !== '.') {
|
|
138
|
+
domain = '.' + domain; // ensure preceeding dot for user set domains
|
|
139
|
+
}
|
|
140
|
+
cookie.domain = domain;
|
|
141
|
+
break;
|
|
142
|
+
|
|
143
|
+
case 'max-age':
|
|
144
|
+
cookie.expires = new Date(Date.now() + (Number(value) || 0) * 1000);
|
|
145
|
+
break;
|
|
146
|
+
|
|
147
|
+
case 'secure':
|
|
148
|
+
cookie.secure = true;
|
|
149
|
+
break;
|
|
150
|
+
|
|
151
|
+
case 'httponly':
|
|
152
|
+
cookie.httponly = true;
|
|
153
|
+
break;
|
|
154
|
+
|
|
155
|
+
default:
|
|
156
|
+
if (!cookie.name) {
|
|
157
|
+
cookie.name = key;
|
|
158
|
+
cookie.value = value;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
return cookie;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Checks if a cookie object is valid for a specified URL
|
|
168
|
+
*
|
|
169
|
+
* @param {Object} cookie Cookie object
|
|
170
|
+
* @param {String} url URL to check for
|
|
171
|
+
* @returns {Boolean} true if cookie is valid for specifiec URL
|
|
172
|
+
*/
|
|
173
|
+
match(cookie, url) {
|
|
174
|
+
let urlparts = urllib.parse(url || '');
|
|
175
|
+
|
|
176
|
+
// check if hostname matches
|
|
177
|
+
// .foo.com also matches subdomains, foo.com does not
|
|
178
|
+
if (
|
|
179
|
+
urlparts.hostname !== cookie.domain &&
|
|
180
|
+
(cookie.domain.charAt(0) !== '.' || ('.' + urlparts.hostname).substr(-cookie.domain.length) !== cookie.domain)
|
|
181
|
+
) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// check if path matches
|
|
186
|
+
let path = this.getPath(urlparts.pathname);
|
|
187
|
+
if (path.substr(0, cookie.path.length) !== cookie.path) {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// check secure argument
|
|
192
|
+
if (cookie.secure && urlparts.protocol !== 'https:') {
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Adds (or updates/removes if needed) a cookie object to the cookie storage
|
|
201
|
+
*
|
|
202
|
+
* @param {Object} cookie Cookie value to be stored
|
|
203
|
+
*/
|
|
204
|
+
add(cookie) {
|
|
205
|
+
let i;
|
|
206
|
+
let len;
|
|
207
|
+
|
|
208
|
+
// nothing to do here
|
|
209
|
+
if (!cookie || !cookie.name) {
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// overwrite if has same params
|
|
214
|
+
for (i = 0, len = this.cookies.length; i < len; i++) {
|
|
215
|
+
if (this.compare(this.cookies[i], cookie)) {
|
|
216
|
+
// check if the cookie needs to be removed instead
|
|
217
|
+
if (this.isExpired(cookie)) {
|
|
218
|
+
this.cookies.splice(i, 1); // remove expired/unset cookie
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
this.cookies[i] = cookie;
|
|
223
|
+
return true;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// add as new if not already expired
|
|
228
|
+
if (!this.isExpired(cookie)) {
|
|
229
|
+
this.cookies.push(cookie);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Checks if two cookie objects are the same
|
|
237
|
+
*
|
|
238
|
+
* @param {Object} a Cookie to check against
|
|
239
|
+
* @param {Object} b Cookie to check against
|
|
240
|
+
* @returns {Boolean} True, if the cookies are the same
|
|
241
|
+
*/
|
|
242
|
+
compare(a, b) {
|
|
243
|
+
return a.name === b.name && a.path === b.path && a.domain === b.domain && a.secure === b.secure && a.httponly === a.httponly;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Checks if a cookie is expired
|
|
248
|
+
*
|
|
249
|
+
* @param {Object} cookie Cookie object to check against
|
|
250
|
+
* @returns {Boolean} True, if the cookie is expired
|
|
251
|
+
*/
|
|
252
|
+
isExpired(cookie) {
|
|
253
|
+
return (cookie.expires && cookie.expires < new Date()) || !cookie.value;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Returns normalized cookie path for an URL path argument
|
|
258
|
+
*
|
|
259
|
+
* @param {String} pathname
|
|
260
|
+
* @returns {String} Normalized path
|
|
261
|
+
*/
|
|
262
|
+
getPath(pathname) {
|
|
263
|
+
let path = (pathname || '/').split('/');
|
|
264
|
+
path.pop(); // remove filename part
|
|
265
|
+
path = path.join('/').trim();
|
|
266
|
+
|
|
267
|
+
// ensure path prefix /
|
|
268
|
+
if (path.charAt(0) !== '/') {
|
|
269
|
+
path = '/' + path;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// ensure path suffix /
|
|
273
|
+
if (path.substr(-1) !== '/') {
|
|
274
|
+
path += '/';
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return path;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
module.exports = Cookies;
|