-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathnode.js
149 lines (124 loc) · 4.16 KB
/
node.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
var AWS = require('../core');
var Stream = AWS.util.nodeRequire('stream').Stream;
var WritableStream = AWS.util.nodeRequire('stream').Writable;
var ReadableStream = AWS.util.nodeRequire('stream').Readable;
require('../http');
/**
* @api private
*/
AWS.NodeHttpClient = AWS.util.inherit({
handleRequest: function handleRequest(httpRequest, httpOptions, callback, errCallback) {
var cbAlreadyCalled = false;
var endpoint = httpRequest.endpoint;
var pathPrefix = '';
if (!httpOptions) httpOptions = {};
if (httpOptions.proxy) {
pathPrefix = endpoint.protocol + '//' + endpoint.hostname;
if (endpoint.port !== 80 && endpoint.port !== 443) {
pathPrefix += ':' + endpoint.port;
}
endpoint = new AWS.Endpoint(httpOptions.proxy);
}
var useSSL = endpoint.protocol === 'https:';
var http = useSSL ? require('https') : require('http');
var options = {
host: endpoint.hostname,
port: endpoint.port,
method: httpRequest.method,
headers: httpRequest.headers,
path: pathPrefix + httpRequest.path
};
if (useSSL && !httpOptions.agent) {
options.agent = this.sslAgent();
}
AWS.util.update(options, httpOptions);
delete options.proxy; // proxy isn't an HTTP option
delete options.timeout; // timeout isn't an HTTP option
var stream = http.request(options, function (httpResp) {
if (cbAlreadyCalled) return; cbAlreadyCalled = true;
callback(httpResp);
httpResp.emit('headers', httpResp.statusCode, httpResp.headers);
});
httpRequest.stream = stream; // attach stream to httpRequest
// timeout support
stream.setTimeout(httpOptions.timeout || 0, function() {
if (cbAlreadyCalled) return; cbAlreadyCalled = true;
var msg = 'Connection timed out after ' + httpOptions.timeout + 'ms';
errCallback(AWS.util.error(new Error(msg), {code: 'TimeoutError'}));
stream.abort();
});
stream.on('error', function() {
if (cbAlreadyCalled) return; cbAlreadyCalled = true;
errCallback.apply(this, arguments);
});
this.writeBody(stream, httpRequest);
return stream;
},
writeBody: function writeBody(stream, httpRequest) {
var body = httpRequest.body;
if (body && WritableStream && ReadableStream) { // progress support
if (!(body instanceof Stream)) body = this.bufferToStream(body);
body.pipe(this.progressStream(stream, httpRequest));
}
if (body instanceof Stream) {
body.pipe(stream);
} else if (body) {
stream.end(body);
} else {
stream.end();
}
},
sslAgent: function sslAgent() {
var https = require('https');
if (!AWS.NodeHttpClient.sslAgent) {
AWS.NodeHttpClient.sslAgent = new https.Agent({rejectUnauthorized: true});
AWS.NodeHttpClient.sslAgent.setMaxListeners(0);
// delegate maxSockets to globalAgent
Object.defineProperty(AWS.NodeHttpClient.sslAgent, 'maxSockets', {
enumerable: true,
get: function() { return https.globalAgent.maxSockets; }
});
}
return AWS.NodeHttpClient.sslAgent;
},
progressStream: function progressStream(stream, httpRequest) {
var numBytes = 0;
var totalBytes = httpRequest.headers['Content-Length'];
var writer = new WritableStream();
writer._write = function(chunk, encoding, callback) {
if (chunk) {
numBytes += chunk.length;
stream.emit('sendProgress', {
loaded: numBytes, total: totalBytes
});
}
callback();
};
return writer;
},
bufferToStream: function bufferToStream(buffer) {
if (!AWS.util.Buffer.isBuffer(buffer)) buffer = new AWS.util.Buffer(buffer);
var readable = new ReadableStream();
var pos = 0;
readable._read = function(size) {
if (pos >= buffer.length) return readable.push(null);
var end = pos + size;
if (end > buffer.length) end = buffer.length;
readable.push(buffer.slice(pos, end));
pos = end;
};
return readable;
},
emitter: null
});
/**
* @!ignore
*/
/**
* @api private
*/
AWS.HttpClient.prototype = AWS.NodeHttpClient.prototype;
/**
* @api private
*/
AWS.HttpClient.streamsApiVersion = ReadableStream ? 2 : 1;