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
150 lines (126 loc) · 3.07 KB
/
part.js
File metadata and controls
150 lines (126 loc) · 3.07 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
149
150
/**
* Module dependencies.
*/
var util = require('util');
var mime = require('mime');
var FormData = require('form-data');
var PassThrough = require('readable-stream/passthrough');
/**
* Initialize a new `Part` for the given `req`.
*
* @param {Request} req
* @api public
* @deprecated pass a readable stream in to `Request#attach()` instead
*/
var Part = function (req) {
PassThrough.call(this);
this._req = req;
this._attached = false;
this._name = null;
this._type = null;
this._header = null;
this._filename = null;
this.once('pipe', this._attach.bind(this));
};
Part = util.deprecate(Part, 'The `Part()` constructor is deprecated. ' +
'Pass a readable stream in to `Request#attach()` instead.');
/**
* Inherit from `PassThrough`.
*/
util.inherits(Part, PassThrough);
/**
* Expose `Part`.
*/
module.exports = Part;
/**
* 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._header) this._header = {};
//this._header[field] = val;
//return this;
throw new TypeError('setting custom form-data part headers is unsupported');
};
/**
* Set _Content-Type_ response header passed through `mime.lookup()`.
*
* Examples:
*
* res.type('html');
* res.type('.html');
*
* @param {String} type
* @return {Part} for chaining
* @api public
*/
Part.prototype.type = function(type){
var lookup = mime.lookup(type);
this._type = lookup;
//this.set('Content-Type', lookup);
return this;
};
/**
* Set the "name" portion for the _Content-Disposition_ header field.
*
* @param {String} name
* @return {Part} for chaining
* @api public
*/
Part.prototype.name = function(name){
this._name = name;
return this;
};
/**
* Set _Content-Disposition_ header field to _attachment_ with `filename`
* and field `name`.
*
* @param {String} name
* @param {String} filename
* @return {Part} for chaining
* @api public
*/
Part.prototype.attachment = function(name, filename){
this.name(name);
if (filename) {
this.type(filename);
this._filename = filename;
}
return this;
};
/**
* Calls `FormData#append()` on the Request instance's FormData object.
*
* Gets called implicitly upon the first `write()` call, or the "pipe" event.
*
* @api private
*/
Part.prototype._attach = function(){
if (this._attached) return;
this._attached = true;
if (!this._name) throw new Error('must call `Part#name()` first!');
// add `this` Stream's readable side as a stream for this Part
if (!this._req._formData) this._req._formData = new FormData();
this._req._formData.append(this._name, this, {
contentType: this._type,
filename: this._filename
});
// restore PassThrough's default `write()` function now that we're setup
this.write = PassThrough.prototype.write;
};
/**
* Write `data` with `encoding`.
*
* @param {Buffer|String} data
* @param {String} encoding
* @return {Boolean}
* @api public
*/
Part.prototype.write = function(){
this._attach();
return this.write.apply(this, arguments);
};