forked from eface2face/mimemessage.js
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEntity.js
197 lines (168 loc) · 5.39 KB
/
Entity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Expose the Entity class.
*/
module.exports = Entity;
/**
* Dependencies.
*/
const rfc2047 = require('rfc2047');
const debug = require('debug')('mimemessage:Entity');
const debugerror = require('debug')('mimemessage:ERROR:Entity');
const grammar = require('./grammar');
const parseHeaderValue = require('./parse').parseHeaderValue;
const encoding = require('./encoding');
debugerror.log = console.warn.bind(console);
function Entity() {
debug('new()');
this.headers = {};
this.internalBody = null;
}
Entity.prototype.contentType = function(value) {
// Get.
if (!value && value !== null) {
return this.headers['Content-Type'];
// Set.
}
if (value) {
this.headers['Content-Type'] = parseHeaderValue(grammar.headerRules['Content-Type'], value);
// Delete.
} else {
delete this.headers['Content-Type'];
}
};
Entity.prototype.contentDisposition = function(value) {
// Get.
if (!value && value !== null) {
return this.headers['Content-Disposition'];
// Set.
}
if (value) {
this.headers['Content-Disposition'] = parseHeaderValue(grammar.headerRules['Content-Disposition'], value);
// Delete.
} else {
delete this.headers['Content-Disposition'];
}
};
Entity.prototype.contentTransferEncoding = function(value) {
const contentTransferEncoding = this.headers['Content-Transfer-Encoding'];
// Get.
if (!value && value !== null) {
return contentTransferEncoding ? contentTransferEncoding.value : undefined;
// Set.
}
if (value) {
this.headers['Content-Transfer-Encoding'] = parseHeaderValue(
grammar.headerRules['Content-Transfer-Encoding'],
value
);
// Delete.
} else {
delete this.headers['Content-Transfer-Encoding'];
}
};
Entity.prototype.header = function(name, value) {
const headername = grammar.headerize(name);
// Get.
if (!value && value !== null) {
if (this.headers[headername]) {
return this.headers[headername].value;
}
// Set.
} else if (value) {
this.headers[headername] = {
value
};
// Delete.
} else {
delete this.headers[headername];
}
};
Object.defineProperty(Entity.prototype, 'body', {
get() {
return this.internalBody;
},
set(body) {
if (body) {
setBody.call(this, body);
} else {
delete this.internalBody;
}
}
});
Entity.prototype.isMultiPart = function() {
const contentType = this.headers['Content-Type'];
return contentType && contentType.type === 'multipart';
};
Entity.prototype.toString = function(options = { noHeaders: false, unicode: false }) {
let raw = '';
const contentType = this.headers['Content-Type'];
const encode = options.unicode ? (x) => x : rfc2047.encode;
if (!options.noHeaders) {
// MIME headers.
const headers = Object.keys(this.headers).map(
(name) => {
const val = this.headers[name].value;
const list = val.split(';').map((val) => val.split('=').map(encode).join('='));
return name + ': ' + list.join(';') + '\r\n';
}
);
raw = headers.join('') + '\r\n';
}
// Body.
if (Array.isArray(this.internalBody)) {
const boundary = contentType.params.boundary;
let i;
const len = this.internalBody.length;
for (i = 0; i < len; i++) {
if (i > 0) {
raw += '\r\n';
}
raw += '--' + boundary + '\r\n' + this.internalBody[i].toString(options);
}
raw += '\r\n--' + boundary + '--';
} else if (typeof this.internalBody === 'string') {
const { value } = this.headers['Content-Transfer-Encoding'] || {};
const { params: { charset = '' } = {} } = this.contentType() || {};
const transform = [];
if (charset.replace(/-/g, '').toLowerCase() === 'utf8') {
transform.push(encoding.encodeUTF8);
}
if (value === 'base64') {
transform.push(encoding.encodeBase64);
} else if (value === 'quoted-printable') {
transform.push(encoding.encodeQP);
}
raw += transform.reduce((body, cb) => cb(body), this.internalBody);
} else if (typeof this.internalBody === 'object') {
raw += JSON.stringify(this.internalBody);
}
return raw;
};
const random16bitHex = () =>
Math.floor(Math.random() * (2 << 15))
.toString(16)
.padStart(4, 0);
const random128bitHex = () =>
new Array(8)
.fill(null)
.map(random16bitHex)
.join('');
const generateBoundary = () => `---------------------${random128bitHex()}`;
/**
* Private API.
*/
function setBody(body) {
const contentType = this.headers['Content-Type'];
this.internalBody = body;
// Multipart internalBody.
if (Array.isArray(body)) {
if (!contentType || contentType.type !== 'multipart') {
this.contentType('multipart/mixed;boundary=' + generateBoundary());
} else if (!contentType.params.boundary) {
this.contentType(contentType.fulltype + ';boundary=' + generateBoundary());
}
// Single internalBody.
} else if (!contentType || contentType.type === 'multipart') {
this.contentType('text/plain;charset=utf-8');
}
}