forked from forwardemail/superagent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart.js
More file actions
149 lines (127 loc) · 2.84 KB
/
part.js
File metadata and controls
149 lines (127 loc) · 2.84 KB
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
/**
* Module dependencies.
*/
var utils = require('./utils');
var Stream = require('stream').Stream;
var mime = require('mime');
var path = require('path');
var basename = path.basename;
/**
* Expose `Part`.
*/
module.exports = Part;
/**
* Initialize a new `Part` for the given `req`.
*
* @param {Request} req
* @api public
*/
function Part(req) {
this.req = req;
this.header = {};
this.headerSent = false;
this.request = req.request();
this.writable = true;
if (!req._boundary) this.assignBoundary();
}
/**
* Inherit from `Stream.prototype`.
*/
Part.prototype.__proto__ = Stream.prototype;
/**
* Assign the initial request-level boundary.
*
* @api private
*/
Part.prototype.assignBoundary = function(){
var boundary = utils.uid(32);
this.req.set('Content-Type', 'multipart/form-data; boundary=' + boundary);
this.req._boundary = boundary;
};
/**
* Set header `field` to `val`.
*
* @param {String} field
* @param {String} val
* @return {Part} for chaining
* @api public
*/
Part.prototype.set = function(field, val){
if (!this._boundary) {
// TODO: formidable bug
if (!this.request._hasFirstBoundary) {
this.request.write('--' + this.req._boundary + '\r\n');
this.request._hasFirstBoundary = true;
} else {
this.request.write('\r\n--' + this.req._boundary + '\r\n');
}
this._boundary = true;
}
this.request.write(field + ': ' + val + '\r\n');
return this;
};
/**
* Set _Content-Type_ response header passed through `mime.lookup()`.
*
* Examples:
*
* res.type('html');
* res.type('.html');
*
* @param {String} type
* @return {Part}
* @api public
*/
Part.prototype.type = function(type){
return this.set('Content-Type', mime.lookup(type));
};
/**
* Set _Content-Disposition_ header field to _form-data_
* and set the _name_ param to the given string.
*
* @param {String} name
* @return {Part}
* @api public
*/
Part.prototype.name = function(name){
this.set('Content-Disposition', 'form-data; name="' + name + '"');
return this;
};
/**
* Set _Content-Disposition_ header field to _attachment_ with `filename`
* and field `name`.
*
* @param {String} name
* @param {String} filename
* @return {Part}
* @api public
*/
Part.prototype.attachment = function(name, filename){
this.type(filename);
this.set('Content-Disposition', 'form-data; name="' + name + '"; filename="' + basename(filename) + '"');
return this;
};
/**
* Write `data` with `encoding`.
*
* @param {Buffer|String} data
* @param {String} encoding
* @return {Boolean}
* @api public
*/
Part.prototype.write = function(data, encoding){
if (!this._headerCRLF) {
this.request.write('\r\n');
this._headerCRLF = true;
}
return this.request.write(data, encoding);
};
/**
* End the part.
*
* @api public
*/
Part.prototype.end = function(){
this.emit('end');
this.complete = true;
};