Skip to content

Commit 9aee412

Browse files
committed
Implements the buffer response handler as Buffer instead of String.
1 parent f88edac commit 9aee412

File tree

7 files changed

+21
-11
lines changed

7 files changed

+21
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* Changed the internal structure of the library.
1818
* Deprecates query.call() in favor of query.request() for the query APIs.
1919
* Deprecates s3.putObject() in favor of s3.putFile().
20-
* Implements new GET response handlers: buffer - returns the response argument as the response body itself; stream - returns the HTTPS response itself which implements node.js's Readable Stream interface.
20+
* Implements new GET response handlers: buffer - the response contains a buffer key with the buffer contents; stream - returns the HTTPS response itself which implements node.js's Readable Stream interface.
2121
* Adds a new s3.putStream() helper for PUT'ing streams to S3.
2222
* Adds a new s3.putBuffer() helper for PUT'ing buffers to S3.
2323
* Unit testing coverage.

lib/Buffer.toByteArray.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Buffer.toByteArray() - Returns an array of bytes from the Buffer argument
3+
* @param buffer
4+
*/
5+
if ( ! Buffer.prototype.toByteArray) {
6+
Buffer.prototype.toByteArray = function () {
7+
return Array.prototype.slice.call(this, 0);
8+
};
9+
}
File renamed without changes.

lib/internals.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ var qs = require('querystring');
1111
var Stream = require('stream').Stream;
1212

1313
/* the internally used modules */
14-
require('./object.watch.js');
14+
require('./Object.watch.js');
15+
require('./Buffer.toByteArray.js')
1516
var cfg = require('./config.js');
1617
var tools = require('./tools.js');
1718

@@ -91,7 +92,7 @@ var makeRequest = function (config, options, body, handler, callback, requestId)
9192
break;
9293
}
9394
}
94-
var data = '';
95+
var data = [];
9596
if (handler != 'xml' && handler != 'buffer' && handler != 'stream' && handler != null) {
9697
try {
9798
if ( ! handler.file) {
@@ -134,7 +135,7 @@ var makeRequest = function (config, options, body, handler, callback, requestId)
134135
var aborted = false;
135136
var request = https.request(options, function (response) {
136137
var parseXml = function (data) {
137-
libxml2js(data, function (error, result) {
138+
libxml2js(new Buffer(data).toString(), function (error, result) {
138139
if (response.statusCode != 200) {
139140
error = new Error('API error with HTTP Code: ' + response.statusCode);
140141
error.headers = response.headers;
@@ -180,16 +181,16 @@ var makeRequest = function (config, options, body, handler, callback, requestId)
180181
case 'xml':
181182
case 'buffer':
182183
case 'stream':
183-
data += chunk;
184+
data = data.concat(chunk.toByteArray());
184185
break;
185186
case null:
186187
if (response.statusCode != 200 && response.statusCode != 204) {
187-
data += chunk;
188+
data = data.concat(chunk.toByteArray());
188189
}
189190
break;
190191
default:
191192
if (response.statusCode != 200) {
192-
data += chunk;
193+
data = data.concat(chunk.toByteArray());
193194
} else {
194195
try {
195196
file.write(chunk);
@@ -213,7 +214,7 @@ var makeRequest = function (config, options, body, handler, callback, requestId)
213214
if (response.statusCode == 200) {
214215
callback(null, {
215216
headers: response.headers,
216-
buffer: data
217+
buffer: new Buffer(data)
217218
});
218219
} else {
219220
parseXml(data);

tests/s3-put-buffer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ s3.putStream(path, buffer, false, {'content-type': 'text/plain'}, function (err,
2020
callbacks.get = true;
2121
assert.ifError(err);
2222
assert.deepEqual(res.headers['content-type'], 'text/plain');
23-
assert.deepEqual(res.buffer, 'bar\n');
23+
assert.deepEqual(res.buffer.toString(), 'bar\n');
2424
s3.del(path, function (err) {
2525
callbacks.del = true;
2626
assert.ifError(err);

tests/s3-put-file-special-chars.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ s3.putFile(path, './data/foo.txt', false, {}, function (err, res) {
1818
callbacks.get = true;
1919
assert.ifError(err);
2020
assert.deepEqual(res.headers['content-type'], 'text/plain');
21-
assert.deepEqual(res.buffer, 'bar\n');
21+
assert.deepEqual(res.buffer.toString(), 'bar\n');
2222
s3.del(path, function (err) {
2323
callbacks.del = true;
2424
assert.ifError(err);

tests/s3-put-stream.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ s3.putStream(path, stream, false, {'content-length': 4, 'content-type': 'text/pl
2020
callbacks.get = true;
2121
assert.ifError(err);
2222
assert.deepEqual(res.headers['content-type'], 'text/plain');
23-
assert.deepEqual(res.buffer, 'bar\n');
23+
assert.deepEqual(res.buffer.toString(), 'bar\n');
2424
s3.del(path, function (err) {
2525
callbacks.del = true;
2626
assert.ifError(err);

0 commit comments

Comments
 (0)