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
110 lines (91 loc) · 1.83 KB
/
part.js
File metadata and controls
110 lines (91 loc) · 1.83 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
/*!
* superagent - Part
* Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var utils = require('./utils')
, Stream = require('stream').Stream;
/**
* 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) {
this.request.write('\r\n--' + this.req._boundary + '\r\n');
this._boundary = true;
}
this.request.write(field + ': ' + val + '\r\n');
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);
};
/**
* Return a new `Part`.
*
* @return {Part}
* @api public
*/
Part.prototype.part = function(){
return this.req.part();
};
/**
* End the request.
*
* @return {Request}
* @api public
*/
Part.prototype.end = function(fn){
return this.req.end(fn);
};