forked from cujojs/rest
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode.js
More file actions
147 lines (118 loc) · 3.66 KB
/
Copy pathnode.js
File metadata and controls
147 lines (118 loc) · 3.66 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
/*
* Copyright 2012-2013 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Jeremy Grelle
* @author Scott Andrews
*/
(function (define) {
'use strict';
define(function (require) {
var parser, http, https, when, UrlBuilder, normalizeHeaderName, httpsExp;
parser = require('url');
http = require('http');
https = require('https');
when = require('when');
UrlBuilder = require('../UrlBuilder');
normalizeHeaderName = require('../util/normalizeHeaderName');
httpsExp = /^https/i;
// TODO remove once Node 0.6 is no longer supported
Buffer.concat = Buffer.concat || function (list, length) {
/*jshint plusplus:false, shadow:true */
// from https://github.com/joyent/node/blob/v0.8.21/lib/buffer.js
if (!Array.isArray(list)) {
throw new Error('Usage: Buffer.concat(list, [length])');
}
if (list.length === 0) {
return new Buffer(0);
} else if (list.length === 1) {
return list[0];
}
if (typeof length !== 'number') {
length = 0;
for (var i = 0; i < list.length; i++) {
var buf = list[i];
length += buf.length;
}
}
var buffer = new Buffer(length);
var pos = 0;
for (var i = 0; i < list.length; i++) {
var buf = list[i];
buf.copy(buffer, pos);
pos += buf.length;
}
return buffer;
};
function node(request) {
var d, options, clientRequest, client, url, headers, entity, response;
request = typeof request === 'string' ? { path: request } : request || {};
response = { request: request };
if (request.canceled) {
response.error = 'precanceled';
return when.reject(response);
}
d = when.defer();
url = new UrlBuilder(request.path || '', request.params).build();
client = url.match(httpsExp) ? https : http;
options = parser.parse(url);
entity = request.entity;
request.method = request.method || (entity ? 'POST' : 'GET');
options.method = request.method;
headers = options.headers = {};
Object.keys(request.headers || {}).forEach(function (name) {
headers[normalizeHeaderName(name)] = request.headers[name];
});
if (!headers['Content-Length']) {
headers['Content-Length'] = entity ? Buffer.byteLength(entity, 'utf8') : 0;
}
request.canceled = false;
request.cancel = function cancel() {
request.canceled = true;
clientRequest.abort();
};
clientRequest = client.request(options, function (clientResponse) {
// Array of Buffers to collect response chunks
var buffers = [];
response.raw = {
request: clientRequest,
response: clientResponse
};
response.status = {
code: clientResponse.statusCode
// node doesn't provide access to the status text
};
response.headers = {};
Object.keys(clientResponse.headers).forEach(function (name) {
response.headers[normalizeHeaderName(name)] = clientResponse.headers[name];
});
clientResponse.on('data', function (data) {
// Collect the next Buffer chunk
buffers.push(data);
});
clientResponse.on('end', function () {
// Create the final response entity
response.entity = buffers.length > 0 ? Buffer.concat(buffers).toString() : '';
buffers = null;
d.resolve(response);
});
});
clientRequest.on('error', function (e) {
response.error = e;
d.reject(response);
});
if (entity) {
clientRequest.write(entity);
}
clientRequest.end();
return d.promise;
}
node.chain = function (interceptor, config) {
return interceptor(node, config);
};
return node;
});
}(
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));